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. QMap iterator Bug??
Forum Updated to NodeBB v4.3 + New Features

QMap iterator Bug??

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 5 Posters 3.0k 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.
  • Christian EhrlicherC Christian Ehrlicher

    @IknowQT said in QMap iterator Bug??:

    Can you tell me in detail what to do?

    I already did:

    Create a local copy of the container you want to iterate over.

    Can you tell me if you have an example code?

    Maybe a c++ book would be a good start

    /edit: it's even described in the Qt documentation: https://doc.qt.io/qt-5/containers.html (Search for '// RIGHT' and '// WRONG')

    I Offline
    I Offline
    IknowQT
    wrote on last edited by
    #9

    @Christian-Ehrlicher

    QMap<QString, CLogin::AccountInfo> test = m_pLogin->getAccountMgr();
    			QMap<QString, CLogin::AccountInfo>::iterator iter;// = test.begin();
    			for (iter = test.begin(); iter != test.end(); ++iter)
    			{
    				UpdateTableWidget(iter);
    			}
    

    Are you telling me to write it like this?
    I tested it and it worked well.

    KroMignonK 1 Reply Last reply
    0
    • I IknowQT

      @Christian-Ehrlicher

      QMap<QString, CLogin::AccountInfo> test = m_pLogin->getAccountMgr();
      			QMap<QString, CLogin::AccountInfo>::iterator iter;// = test.begin();
      			for (iter = test.begin(); iter != test.end(); ++iter)
      			{
      				UpdateTableWidget(iter);
      			}
      

      Are you telling me to write it like this?
      I tested it and it worked well.

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #10

      @IknowQT said in QMap iterator Bug??:

      Are you telling me to write it like this?

      What @Christian-Ehrlicher is telling you, is that you are creating 2 different container.
      Each time you are calling m_pLogin->getAccountMgr(), it will return a QMap instance.
      So each one has a different begin and end.
      So you are comparing things which cannot be compared.

      But yes, this is the right way to do, as it is given in Qt documentation:

      // store local copy of the map
      auto &test = m_pLogin->getAccountMgr();
      // iterate through the map
      for (auto iter = test.begin(); iter != test.end(); ++iter)
      {
          UpdateTableWidget(iter);
      }
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #11

        @KroMignon said in QMap iterator Bug??:

        So each one has a different begin and end.

        No, it's even worse - as I said above the two iterators are dangling because the returned containers are temporaries.

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

        I 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          @KroMignon said in QMap iterator Bug??:

          So each one has a different begin and end.

          No, it's even worse - as I said above the two iterators are dangling because the returned containers are temporaries.

          I Offline
          I Offline
          IknowQT
          wrote on last edited by
          #12

          @Christian-Ehrlicher

          I think this topic is endless. Can you post some concrete example code?

          jsulmJ KroMignonK 2 Replies Last reply
          0
          • I IknowQT

            @Christian-Ehrlicher

            I think this topic is endless. Can you post some concrete example code?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #13

            @IknowQT said in QMap iterator Bug??:

            I think this topic is endless. Can you post some concrete example code?

            You already have it and you even wrote that it is working...

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

            1 Reply Last reply
            4
            • I IknowQT

              @Christian-Ehrlicher

              I think this topic is endless. Can you post some concrete example code?

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #14

              @IknowQT said in QMap iterator Bug??:

              I think this topic is endless. Can you post some concrete example code?

              Why endless? All has been explained.
              It is not a QMap iterator bug. It is your code which is wrong.
              Each time you are calling m_pLogin->getAccountMgr() is returns a new QMap instance.
              Your code has many errors:

              • first : in the initialization statement in the for loop you are setting up iter is a begin of the return QMap.
              • second: this return QMap is temporary, which means it is destroyed directly after usage ==> iter becomes a dangling pointer!!!
              • third: in the condition statement, comparing iter with another QMap instance (which also is temporary!!)

              Solution for this ==> learn C++ basics like variable/objects lifetime! (cf. https://en.cppreference.com/w/cpp/language/lifetime)

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              JoeCFDJ 1 Reply Last reply
              4
              • KroMignonK KroMignon

                @IknowQT said in QMap iterator Bug??:

                I think this topic is endless. Can you post some concrete example code?

                Why endless? All has been explained.
                It is not a QMap iterator bug. It is your code which is wrong.
                Each time you are calling m_pLogin->getAccountMgr() is returns a new QMap instance.
                Your code has many errors:

                • first : in the initialization statement in the for loop you are setting up iter is a begin of the return QMap.
                • second: this return QMap is temporary, which means it is destroyed directly after usage ==> iter becomes a dangling pointer!!!
                • third: in the condition statement, comparing iter with another QMap instance (which also is temporary!!)

                Solution for this ==> learn C++ basics like variable/objects lifetime! (cf. https://en.cppreference.com/w/cpp/language/lifetime)

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by JoeCFD
                #15

                @KroMignon if m_pLogin->getAccountMgr() returns const Whatever &, his code will work as well. But it is not great to code like he does. I think the following loop for map may be simpler

                    std::map<std::string, int > test_map;
                    test_map[ "test1" ] = 1;
                    test_map[ "test2" ] = 2;
                    for ( const std::pair< std::string, int > & p : test_map ) {
                        std::cout << p.first << '\t' << p.second << std::endl;
                    }
                
                KroMignonK 1 Reply Last reply
                0
                • JoeCFDJ JoeCFD

                  @KroMignon if m_pLogin->getAccountMgr() returns const Whatever &, his code will work as well. But it is not great to code like he does. I think the following loop for map may be simpler

                      std::map<std::string, int > test_map;
                      test_map[ "test1" ] = 1;
                      test_map[ "test2" ] = 2;
                      for ( const std::pair< std::string, int > & p : test_map ) {
                          std::cout << p.first << '\t' << p.second << std::endl;
                      }
                  
                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #16

                  @JoeCFD said in QMap iterator Bug??:

                  if m_pLogin->getAccountMgr() returns const Whatever &, his code will work as well.

                  Yes, but that was not the point (I guess)

                  I think the following loop for map may be simpler

                  To be honest, there is no real difference. You are using std::map instead of QMap, that's all.
                  It is only a question of taste... Preferring std::map over QMap.
                  There is no need to use QMap::iterator to iterate through a QMap, it is only a possibility ;)

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  JoeCFDJ 1 Reply Last reply
                  2
                  • KroMignonK KroMignon

                    @JoeCFD said in QMap iterator Bug??:

                    if m_pLogin->getAccountMgr() returns const Whatever &, his code will work as well.

                    Yes, but that was not the point (I guess)

                    I think the following loop for map may be simpler

                    To be honest, there is no real difference. You are using std::map instead of QMap, that's all.
                    It is only a question of taste... Preferring std::map over QMap.
                    There is no need to use QMap::iterator to iterate through a QMap, it is only a possibility ;)

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by
                    #17

                    @KroMignon not only a taste issue. I believe std containers may be better optimized than Qt containers in most cases.

                    Christian EhrlicherC KroMignonK 2 Replies Last reply
                    0
                    • JoeCFDJ JoeCFD

                      @KroMignon not only a taste issue. I believe std containers may be better optimized than Qt containers in most cases.

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #18

                      @JoeCFD said in QMap iterator Bug??:

                      I believe

                      ... so nonsense without any real background.
                      And the above usecase which the topic is about (and your posts simply ignore) would be much slower with std::map than QMap since the contents of the std::map would have been copied.

                      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
                      3
                      • JoeCFDJ JoeCFD

                        @KroMignon not only a taste issue. I believe std containers may be better optimized than Qt containers in most cases.

                        KroMignonK Offline
                        KroMignonK Offline
                        KroMignon
                        wrote on last edited by
                        #19

                        @JoeCFD said in QMap iterator Bug??:

                        I believe std containers may be better optimized than Qt containers in most cases.

                        Not according to this benchmark done by woboq => https://woboq.com/blog/qmap_qhash_benchmark.html
                        or this stackoverflow entry => https://stackoverflow.com/questions/1668259/stl-or-qt-containers

                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                        JoeCFDJ 1 Reply Last reply
                        4
                        • KroMignonK KroMignon

                          @JoeCFD said in QMap iterator Bug??:

                          I believe std containers may be better optimized than Qt containers in most cases.

                          Not according to this benchmark done by woboq => https://woboq.com/blog/qmap_qhash_benchmark.html
                          or this stackoverflow entry => https://stackoverflow.com/questions/1668259/stl-or-qt-containers

                          JoeCFDJ Offline
                          JoeCFDJ Offline
                          JoeCFD
                          wrote on last edited by
                          #20

                          @KroMignon said in QMap iterator Bug??:

                          @JoeCFD said in QMap iterator Bug??:

                          I believe std containers may be better optimized than Qt containers in most cases.

                          Not according to this benchmark done by woboq => https://woboq.com/blog/qmap_qhash_benchmark.html <=== done in 2012 ages old.
                          or this stackoverflow entry => https://stackoverflow.com/questions/1668259/stl-or-qt-containers read comments: 35

                          Christian EhrlicherC 1 Reply Last reply
                          0
                          • JoeCFDJ JoeCFD

                            @KroMignon said in QMap iterator Bug??:

                            @JoeCFD said in QMap iterator Bug??:

                            I believe std containers may be better optimized than Qt containers in most cases.

                            Not according to this benchmark done by woboq => https://woboq.com/blog/qmap_qhash_benchmark.html <=== done in 2012 ages old.
                            or this stackoverflow entry => https://stackoverflow.com/questions/1668259/stl-or-qt-containers read comments: 35

                            Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by Christian Ehrlicher
                            #21

                            @JoeCFD When you're at the point where you really need to worry about a speed difference between those implementations you should write your own implementation of the problematic container. You will never notice if a running program is using c++ or Qt or boost or whatever containers.

                            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
                            3

                            • Login

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