4

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?

Borrimoro
  • 529
  • 3
  • 9
  • 19

2 Answers2

3

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:

Community
  • 1
  • 1
Jan Kundrát
  • 3,700
  • 1
  • 18
  • 29
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

Komgcn
  • 323
  • 2
  • 12
  • for example I want to store a list of students' maths score sorted by the way I insert them. First I would construct a QPair for every student, then I insert them in a QList. When I need the "queue" ability of it, I can use a for loop and loop through the QList, when I need the "hash" ability, I will loop through the QList and look at the QPair.first => student_name, if matches, returns the QPair.second=>score – Komgcn Sep 13 '18 at 01:46
  • This doesn't provide features e.g. insert/lookup by key, of course, but it does allow for the preservation of k/v pairs in a specific order. – BuvinJ Nov 11 '21 at 13:42