Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-117757

QMultiHash::insert() does not return a correct iterator to the inserted element

    XMLWordPrintable

Details

    • All
    • 861588472 (dev)

    Description

      It looks like QMultiMap::insert() does not return the correct iterator to the newly inserted element. At least not when the hash function is weak. It looks like only the first used key is stored in the hash.

      Here a small example:

      struct key {
          bool operator==(const key& other) const {
              return x == other.x;
          }
          int x = 0;
          int y = 0;
      };
      
      inline size_t qHash(const key& k) {
          return qHash(k.x);
      }
      template<>
      struct std::hash<key>
      {
          std::size_t operator()(const key& k) const noexcept
          {
              return qHash(k);
          }
      };
      
      int main(int argc, char* argv[])
      {
          qDebug() << "QMultiHash";
          {
              QMultiHash<key, int> m;
              m.insert({ 1, 1 }, 1);
              auto it = m.insert({ 1, 2 }, 2);
              qInfo() << "Key:" << it.key().x << it.key().y << ", value: " << it.value();
              qInfo() << "Dumping container:";
              for (auto it = m.begin(); it != m.end(); ++it) {
                  qInfo() << "Key:" << it.key().x << it.key().y << ", value: " << it.value();
              }
          }
          qDebug() << "";
          qDebug() << "std::unordered_multimap";
          {
              std::unordered_multimap<key, int> u;
              u.insert(std::make_pair<key, int>({ 1, 1 }, 1));
              auto it = u.insert(std::make_pair<key, int>({ 1, 2 }, 2));
              qInfo() << "Key:" << it->first.x << it->first.y << ", value: " << it->second;
              qInfo() << "Dumping container:";
              for (auto it = u.begin(); it != u.end(); ++it) {
                  qInfo() << "Key:" << it->first.x << it->first.y << ", value: " << it->second;
              }
          }
          return 0;
      }
      

      Return value:

      QMultiHash
      Key: 1 1 , value:  2
      Dumping container:
      Key: 1 1 , value:  2
      Key: 1 1 , value:  1
      
      std::unordered_multimap
      Key: 1 2 , value:  2
      Dumping container:
      Key: 1 1 , value:  1
      Key: 1 2 , value:  2
      

      The documentation of QMultiHash/QHash/QMultiMap/QMap::insert() also lacks the description of the return value.

      Original bug report from the forum: https://forum.qt.io/topic/150412

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            thiago Thiago Macieira
            chehrlic Christian Ehrlicher
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes