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. What does QMultiHash::insert(key, value) return?
Forum Updated to NodeBB v4.3 + New Features

What does QMultiHash::insert(key, value) return?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 414 Views 1 Watching
  • 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.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on last edited by Violet Giraffe
    #1

    I used to think Qt has a good quality documentation, but apparently not always. What does this method return?

    I'm asking because my code relies on it returning an iterator to the newly inserted item, which was the case in Qt 5.15, but with Qt 6.5 it returns an iterator to the first item. Quite a breaking change, as you can imagine.
    Seems like it might be returning the first item with the same key, rather than the newly inserted item. The important part is that the docs don't say what the return value is.

    Christian EhrlicherC 1 Reply Last reply
    0
    • V Violet Giraffe

      Apologies, I've made a mistake that's probably an important distinction: my question is about QMultiHash, not QMultiMap. What is QMultiHash::insert() supposed to return? The docs similarly don't mention the return value.

      Here's the demo. This code returns 2 with Qt 5.15 (expected) and 1 with Qt 6.5 (unexpected, and cost me over an hour of debugging).

      #include <QMultiHash>
      #include <QDebug>
      
      struct key {
      	bool operator==(const key& other) const {
      		return x == other.x;
      	}
      
      	int x;
      	int y;
      };
      
      inline uint qHash(const key& k) {
      	return qHash(k.x);
      }
      
      int main(int argc, char *argv[])
      {
      	QMultiHash<key, int> m;
      
      	m.insert({1, 1}, 1);
      	const auto it = m.insert({1, 2}, 2);
      	qInfo() << it.key().y;
      
      	return 0;
      }
      
      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #6

      The result for Qt6 is the one I expected from reading the code - it returns the iterator to the first element for the key (in the bucket).
      Will create a bug report for the documentation update.

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

      V 1 Reply Last reply
      1
      • V Violet Giraffe

        I used to think Qt has a good quality documentation, but apparently not always. What does this method return?

        I'm asking because my code relies on it returning an iterator to the newly inserted item, which was the case in Qt 5.15, but with Qt 6.5 it returns an iterator to the first item. Quite a breaking change, as you can imagine.
        Seems like it might be returning the first item with the same key, rather than the newly inserted item. The important part is that the docs don't say what the return value is.

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

        Since it's using std::multimap internally: https://en.cppreference.com/w/cpp/container/multimap/insert

        But yes, the documentation should be updated.

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

        V 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          Since it's using std::multimap internally: https://en.cppreference.com/w/cpp/container/multimap/insert

          But yes, the documentation should be updated.

          V Offline
          V Offline
          Violet Giraffe
          wrote on last edited by
          #3

          @Christian-Ehrlicher, weird: it says "Returns an iterator to the inserted element", but that's not what is actually returned.

          Christian EhrlicherC 1 Reply Last reply
          0
          • V Violet Giraffe

            @Christian-Ehrlicher, weird: it says "Returns an iterator to the inserted element", but that's not what is actually returned.

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

            The please provide a testcase.
            Note: Qt5 and Qt6 may differ here.

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

            V 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              The please provide a testcase.
              Note: Qt5 and Qt6 may differ here.

              V Offline
              V Offline
              Violet Giraffe
              wrote on last edited by Violet Giraffe
              #5

              Apologies, I've made a mistake that's probably an important distinction: my question is about QMultiHash, not QMultiMap. What is QMultiHash::insert() supposed to return? The docs similarly don't mention the return value.

              Here's the demo. This code returns 2 with Qt 5.15 (expected) and 1 with Qt 6.5 (unexpected, and cost me over an hour of debugging).

              #include <QMultiHash>
              #include <QDebug>
              
              struct key {
              	bool operator==(const key& other) const {
              		return x == other.x;
              	}
              
              	int x;
              	int y;
              };
              
              inline uint qHash(const key& k) {
              	return qHash(k.x);
              }
              
              int main(int argc, char *argv[])
              {
              	QMultiHash<key, int> m;
              
              	m.insert({1, 1}, 1);
              	const auto it = m.insert({1, 2}, 2);
              	qInfo() << it.key().y;
              
              	return 0;
              }
              
              Christian EhrlicherC 1 Reply Last reply
              0
              • V Violet Giraffe

                Apologies, I've made a mistake that's probably an important distinction: my question is about QMultiHash, not QMultiMap. What is QMultiHash::insert() supposed to return? The docs similarly don't mention the return value.

                Here's the demo. This code returns 2 with Qt 5.15 (expected) and 1 with Qt 6.5 (unexpected, and cost me over an hour of debugging).

                #include <QMultiHash>
                #include <QDebug>
                
                struct key {
                	bool operator==(const key& other) const {
                		return x == other.x;
                	}
                
                	int x;
                	int y;
                };
                
                inline uint qHash(const key& k) {
                	return qHash(k.x);
                }
                
                int main(int argc, char *argv[])
                {
                	QMultiHash<key, int> m;
                
                	m.insert({1, 1}, 1);
                	const auto it = m.insert({1, 2}, 2);
                	qInfo() << it.key().y;
                
                	return 0;
                }
                
                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                The result for Qt6 is the one I expected from reading the code - it returns the iterator to the first element for the key (in the bucket).
                Will create a bug report for the documentation update.

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

                V 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  The result for Qt6 is the one I expected from reading the code - it returns the iterator to the first element for the key (in the bucket).
                  Will create a bug report for the documentation update.

                  V Offline
                  V Offline
                  Violet Giraffe
                  wrote on last edited by Violet Giraffe
                  #7

                  @Christian-Ehrlicher
                  Thank you very much!

                  To me this behavior makes little sense because I can't think of what it could be useful for (unlike the behavior of Qt 5). But that doesn't matter; what matters is that programmers should know how to the use the interface correctly and what to expect - your bug report will solve that.

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • V Violet Giraffe has marked this topic as solved on
                  • V Violet Giraffe

                    @Christian-Ehrlicher
                    Thank you very much!

                    To me this behavior makes little sense because I can't think of what it could be useful for (unlike the behavior of Qt 5). But that doesn't matter; what matters is that programmers should know how to the use the interface correctly and what to expect - your bug report will solve that.

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

                    https://bugreports.qt.io/browse/QTBUG-117757

                    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
                    2

                    • Login

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