Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Looking for an alternative approach to avoid "clazy-container-anti-pattern" in an if-clause

    General and Desktop
    4
    9
    569
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      devjb last edited by

      I get a warning allocating an unneeded temporary container from a code snippet like this:

      		if (QMultiMap<T,U>().keys(someVariableOfTypeU).isEmpty()) {
      			[...]
      		}
      

      I wonder how I can change the if clause to avoid that unnecessary allocation.

      I hope I elaborated enough by providing that pseudo-codish example above.

      Thanks in advance

      1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        use Q(Multi)Map::key()

        Qt has to stay free or it will die.
        Online Installer direct download: https://download.qt.io/official_releases/online_installers/

        1 Reply Last reply Reply Quote 0
        • D
          devjb last edited by devjb

          Thanks for the reply. However, the documentation for const Key QMap::key(const T &value, const Key &defaultKey = Key()) const states:

          Returns the first key with value value, or defaultKey if the map contains no item with value value. If no defaultKey is provided the function returns a default-constructed key.

          How am I supposed to distinguish between not found and default key when a default-constructed key might be a valid key in the map?

          1 Reply Last reply Reply Quote 0
          • Christian Ehrlicher
            Christian Ehrlicher Lifetime Qt Champion last edited by

            A default-constructed key should not be a valid key :)

            Qt has to stay free or it will die.
            Online Installer direct download: https://download.qt.io/official_releases/online_installers/

            1 Reply Last reply Reply Quote 0
            • D
              devjb last edited by

              Given that constraint, okay. Can you point to any source that explains, why exactly a default-constructed key should not be a valid key, or why this is considered bad style?

              1 Reply Last reply Reply Quote 0
              • VRonin
                VRonin last edited by VRonin

                There is no optimisation behind QMap::key so you can just do it manually:

                auto keyIter = map.constBegin(), iEnd = map.constEnd();
                for(;keyIter !=iEnd && keyIter.value() != someVariableOfTypeU;++keyIter){}
                if(keyIter ==iEnd){
                ///[...]
                }
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  Hi,

                  Wouldn't QMap::find get you what you want ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply Reply Quote 0
                  • D
                    devjb last edited by devjb

                    Thanks,
                    QMultiMap::find() searches by key and value, I need to know whether one specific value is somewhere in the map, no matter which key(s) it is associated with.

                    according to @VRonin's suggestion, I simply iterate through the whole map manually now, just incrementing a counter on ocurrence. This avoids creating an extra container for the task and should not be any slower than QMultiMap::keys().

                    1 Reply Last reply Reply Quote 0
                    • SGaist
                      SGaist Lifetime Qt Champion last edited by

                      My bad, I misunderstood your search pattern.

                      From a reading point of view, I would have gone with std::find_if unless you want all the possible key that contains the value.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply Reply Quote 1
                      • First post
                        Last post