Results 1 to 3 of 3

Thread: about QHash elements order

  1. #1
    Join Date
    May 2006
    Posts
    7
    Thanks
    2

    Default about QHash elements order

    Hi all, I put some elements in the QHash in an order, but the retrieve order was changed while I use it's iterator. Need help !

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: about QHash elements order

    QHash uses a tree to store elements, so that the lookup time is optimial, so the order of elements is not preserved. If you need the order of the elements to be maintained, use QMap instead, just be prepared for slower lookups.

  3. The following user says thank you to wysota for this useful post:

    bruce1007 (26th August 2006)

  4. #3
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: about QHash elements order

    That's the way QHash operates. Why? Because it's a hash! To quote the documentation: "QHash is unordered, so an iterator's sequence cannot be assumed to be predictable. If ordering by key is required, use a QMap."

    For more information, lookup hashes and maps in your nearest algorithms and data structures textbook. If you don't have one, get one, and consult wikipedia in the meantime.

  5. The following user says thank you to Brandybuck for this useful post:

    bruce1007 (26th August 2006)

Similar Threads

  1. Replies: 1
    Last Post: 26th February 2006, 05:52
  2. Changing the order of columns in QTreeView
    By johnny_sparx in forum Qt Programming
    Replies: 1
    Last Post: 15th February 2006, 00:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.