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. QHash ordered, not because
Forum Updated to NodeBB v4.3 + New Features

QHash ordered, not because

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.4k 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.
  • H Offline
    H Offline
    homer0123456789
    wrote on last edited by
    #1

    Good afternoon, I have the following code and I returned the tickets I ordered alphabetically, like a QMap, any idea of the problem?, Thanks.

    @
    QHash<QString, int> hash;
    hash["one"] = 1;
    hash["three"] = 3;
    hash["seven"] = 7;

    QHashIterator<QString, int> i(hash);
    while (i.hasNext()) {
        i.next();
        qDebug() << i.key() << ": " << i.value();
    }
    

    @

    exit:
    "one" : 1
    "seven" : 7
    "three" : 3

    [edit: added missing coding tags @ SGaist]

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on last edited by
      #2

      So you are wondering why the items of a QHash are "out of order"?

      It's a fundamental property of a Hash map that items are stored in an arbitrary order. That's the price you have to pay for the fast lookup that the Hash map provides. If you need the keys to be stored in ascending order, use a QMap.

      --

      From the manual:

      bq. When iterating over a QMap, the items are always sorted by key. With QHash, the items are arbitrarily ordered.

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      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