View Full Version : about QHash elements order

25th August 2006, 05:19
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 !


25th August 2006, 08:14
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.

25th August 2006, 08:17
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.