Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Looking for an alternative approach to avoid "clazy-container-anti-pattern" in an if-clause

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.2k Views
  • 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 Offline
    D Offline
    devjb
    wrote on last edited by
    #1

    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
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      use Q(Multi)Map::key()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • D Offline
        D Offline
        devjb
        wrote on last edited by devjb
        #3

        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
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • D Offline
            D Offline
            devjb
            wrote on last edited by
            #5

            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
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              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
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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
                0
                • D Offline
                  D Offline
                  devjb
                  wrote on last edited by devjb
                  #8

                  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
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    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
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved