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. I wish that container.contains() would return the actual value...

I wish that container.contains() would return the actual value...

Scheduled Pinned Locked Moved Solved General and Desktop
optimization
9 Posts 5 Posters 1.3k 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
    Dariusz
    wrote on 5 Aug 2019, 07:46 last edited by
    #1

    Hey...

    Am I the only one who finds doing this :

    if(map.cotnains(key)){
        auto *item = map[key];
       map.remove(key);
       delete item;
    }
    

    Could have been handled by

    if(auto *item = map.contains(key)){
        map.remove(key);
        delete item;
        }
    

    And so on and so forth?
    1 Less function call but on a large scale, it might help?

    J J 2 Replies Last reply 5 Aug 2019, 07:50
    0
    • D Dariusz
      5 Aug 2019, 07:46

      Hey...

      Am I the only one who finds doing this :

      if(map.cotnains(key)){
          auto *item = map[key];
         map.remove(key);
         delete item;
      }
      

      Could have been handled by

      if(auto *item = map.contains(key)){
          map.remove(key);
          delete item;
          }
      

      And so on and so forth?
      1 Less function call but on a large scale, it might help?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 5 Aug 2019, 07:50 last edited by jsulm 8 May 2019, 07:50
      #2

      @dariusz It would be counter-intuitive, because "contains" does what it means: it tells you whether container contains something. A simple yes/no answer. Also, what should it return if there is no such element?
      Last but not least there is https://doc.qt.io/qt-5/qvector.html#value or https://doc.qt.io/qt-5/qmap.html#value which does what you want...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply 5 Aug 2019, 08:53
      3
      • J jsulm
        5 Aug 2019, 07:50

        @dariusz It would be counter-intuitive, because "contains" does what it means: it tells you whether container contains something. A simple yes/no answer. Also, what should it return if there is no such element?
        Last but not least there is https://doc.qt.io/qt-5/qvector.html#value or https://doc.qt.io/qt-5/qmap.html#value which does what you want...

        D Offline
        D Offline
        Dariusz
        wrote on 5 Aug 2019, 08:53 last edited by Dariusz 8 May 2019, 08:53
        #3

        @jsulm said in I wish that container.contains() would return the actual value...:

        @dariusz It would be counter-intuitive, because "contains" does what it means: it tells you whether container contains something. A simple yes/no answer. Also, what should it return if there is no such element?

        I was thinking of it lately, I suppose containsPtr(), would return either nullptr=false or item = true ?

        Last but not least there is https://doc.qt.io/qt-5/qvector.html#value or https://doc.qt.io/qt-5/qmap.html#value which does what you want...

        Heee I never thought of using value()! So if its map of Ptrs, would default value return be a nullptr? As if he just make new item from default constructor then thats no help, I will have to test if the item is "valid" and then delete if its not mhmmm

        1 Reply Last reply
        1
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 5 Aug 2019, 09:06 last edited by
          #4

          Hi,

          The function takes two parameters. You can pass the default value you want to get if the key is not found in the map.

          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
          4
          • D Dariusz
            5 Aug 2019, 07:46

            Hey...

            Am I the only one who finds doing this :

            if(map.cotnains(key)){
                auto *item = map[key];
               map.remove(key);
               delete item;
            }
            

            Could have been handled by

            if(auto *item = map.contains(key)){
                map.remove(key);
                delete item;
                }
            

            And so on and so forth?
            1 Less function call but on a large scale, it might help?

            J Offline
            J Offline
            JKSH
            Moderators
            wrote on 5 Aug 2019, 23:50 last edited by JKSH 8 Jun 2019, 01:25
            #5

            @dariusz said in I wish that container.contains() would return the actual value...:

            if(map.cotnains(key)){
                auto *item = map[key];
               map.remove(key);
               delete item;
            }
            

            This whole block can be replaced by 1 line:

            delete map.take(key);
            

            take() returns the item and removes it from the map. If the map does not contain key, take() returns a default value (nullptr, in your case). It is safe to delete nullptr as long as you don't use a non-standard deallocator.

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            11
            • D Offline
              D Offline
              Dariusz
              wrote on 7 Aug 2019, 07:40 last edited by
              #6

              Thats actually pretty awesome... for deleting! Neat thank you!

              What about this scenario?

                  if (map.contains(key)) {
                      map[key] = "do Stuff";
                  } else {
                      qDebug() << "invalid key ? ";
                  }
              

              Any better way of solving this double search ?

              TIA!

              C 1 Reply Last reply 7 Aug 2019, 08:04
              0
              • D Dariusz
                7 Aug 2019, 07:40

                Thats actually pretty awesome... for deleting! Neat thank you!

                What about this scenario?

                    if (map.contains(key)) {
                        map[key] = "do Stuff";
                    } else {
                        qDebug() << "invalid key ? ";
                    }
                

                Any better way of solving this double search ?

                TIA!

                C Online
                C Online
                Chris Kawa
                Lifetime Qt Champion
                wrote on 7 Aug 2019, 08:04 last edited by
                #7

                Any better way of solving this double search ?

                Yes, the classic stl style:

                auto it = map.find(key);
                if (it != map.end()) {
                   *it = "do Stuff";
                }
                else {
                   qDebug() << "invalid key ? ";
                }
                

                If you don't like that it's one line longer you can use c++17:

                if (auto it = map.find(key); it != map.end()) {
                
                1 Reply Last reply
                7
                • D Offline
                  D Offline
                  Dariusz
                  wrote on 7 Aug 2019, 08:57 last edited by Dariusz 8 Jul 2019, 08:57
                  #8

                  Amazing thank you! :- D

                  I'm a tad handicapped with Qt being so nice and me not knowing 60% of STL...

                  Now just gotta rewrite few...hundreds...of lines of code :D

                  C 1 Reply Last reply 7 Aug 2019, 09:08
                  0
                  • D Dariusz
                    7 Aug 2019, 08:57

                    Amazing thank you! :- D

                    I'm a tad handicapped with Qt being so nice and me not knowing 60% of STL...

                    Now just gotta rewrite few...hundreds...of lines of code :D

                    C Online
                    C Online
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on 7 Aug 2019, 09:08 last edited by
                    #9

                    @dariusz said

                    me not knowing 60% of STL

                    I would concentrate on fixing that. You're missing a large chunk of ready to use stuff and you'll be reinventing a lot of wheels without it.

                    1 Reply Last reply
                    5

                    9/9

                    7 Aug 2019, 09:08

                    • Login

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