简体   繁体   中英

QMap but without sorting by key

I need structure like QMap but without sorting on keys, so if I insert item there first I can count that this item will be before all others. And insert pair before or after specified element. Does Qt have such?

QMap is implemented as a tree, which means that the insertion order does not matter. It appears that you are looking for a queue . However, if you need a container which can be iterated in both insertion order and at the same time accessed through a specific key, then Qt has no such structure for you.

These could help you:

I use a

QList<QPair<key,value>> 

to achieve this. But look up consumes more time as you will need to loop through the QList and use the QPair.first to look for the item you want.

edit: if you dont need it to interact with other API alot, you can use QVector to replace QList which is faster according to Qt official

Is QHash suitable for it?

QHash provides very similar functionality to QMap. The differences are:

  • QHash provides faster lookups than QMap. (See Algorithmic Complexity for details.)
  • When iterating over a QMap, the items are always sorted by key. With QHash, the items are arbitrarily ordered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM