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. Question about QMultiHash:
Forum Updated to NodeBB v4.3 + New Features

Question about QMultiHash:

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 470 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.
  • V Offline
    V Offline
    Volker D
    wrote on last edited by Volker D
    #1

    Hallo,

    referring to https://doc.qt.io/qt-6/qmultihash.html

    Quote: "Unlike QMultiMap, QMultiHash does not provide and ordering of
    the inserted items. The only guarantee is that items that share the
    same key will appear consecutively, from the most recently to the
    least recently inserted value."

    and Quote: "A more efficient approach is to call find() to get the
    STL-style iterator for the first item with a key and iterate from
    there:

    QMultiHash<QString, int>::iterator i = hash.find("plenty");
    while (i != hash.end() && i.key() == "plenty") {
         cout << i.value() << Qt::endl;
         ++i;
    }
    

    "

    So if the items are "consecutively", isn't it much faster this way: (or
    are there still items with other keys between?)

    QMultiHash<QString, int>::iterator i = hash.find("plenty");
    while (i != hash.end()) {
         if(i.key() != "plenty"){
           break;
         }
         cout << i.value() << Qt::endl;
         ++i;
    }
    

    If i am not missing items by that, the documention should be updated.

    Best Regards
    Volker

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

      Can you please format your code - currently it's unreadable.
      From a first sight I don't see a difference between the first and second code piece.

      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
      • V Offline
        V Offline
        Volker D
        wrote on last edited by Volker D
        #3

        Ohh.. Sorry. Fixed.
        Maybe it doesn't work like this, since there might be keys with the same hash and there are in between?
        In that case the "consecutive" word is a bit confusing in the documentation and in that case i ask myself if there isn't a way to get fast a better end. Something like:

        QMultiHash<QString, int>::iterator iend = hash.findNextWithOtherHashAfter("plenty");
        
        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I still see no difference between the first and the second code piece - it does exactly the same.

          You can't get fast to the end since there is a 'bucket' (container) for every key / the calculated hash with all the elements. So the lookup for the items with the same key is O(n) which is why you should a) avoid key collisions / use a good hash functions and b) don't add too much items with the same key when you need fast lookup of a single value.

          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
          1
          • V Offline
            V Offline
            Volker D
            wrote on last edited by
            #5

            hmmm... If it will give the same results, then my variant will be faster, since it doesn't run to the end.

            But i fear my variant is wrong, since the items are not consecutive if 2 >different< keys produce the same hash. In that case the word "consecutive" in the documentation is wrong.

            Christian EhrlicherC 1 Reply Last reply
            0
            • V Volker D

              hmmm... If it will give the same results, then my variant will be faster, since it doesn't run to the end.

              But i fear my variant is wrong, since the items are not consecutive if 2 >different< keys produce the same hash. In that case the word "consecutive" in the documentation is wrong.

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

              @Volker-D said in Question about QMultiHash::

              then my variant will be faster, since it doesn't run to the end.

              Again: the two code pieces are exactly the same

              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
              • V Offline
                V Offline
                Volker D
                wrote on last edited by
                #7

                Ahh... Sorry. I got it now. I was wrong. Thank you very much.

                1 Reply Last reply
                0

                • Login

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