QMap but without sorting by key

拜拜、爱过 提交于 2019-12-10 20:32:57

问题


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?


回答1:


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:

  • A std::map that keep track of the order of insertion?
  • Make Map Key Sorted According To Insert Sequence



回答2:


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



来源:https://stackoverflow.com/questions/16101952/qmap-but-without-sorting-by-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!