PDA

View Full Version : QMap Issue



ManuMies
9th November 2010, 10:13
I have QMap<MyClass*, bool> and I need to remove the oldest item. Can someone provide the code to do it?

I tried removing last Key and first Key, but it still doesn't work properly.

Does it sort the map somehow? And if it does, is there a way to prevent it.

tbscope
9th November 2010, 10:27
Does it sort the map somehow? And if it does, is there a way to prevent it.

http://doc.qt.nokia.com/4.7/qmap.html

ManuMies
9th November 2010, 11:51
Thanks, so QHash is answer to my problem. Solved. :)

Added after 1 12 minutes:

Nope, that didn't work. Now the list is indeed in random order, but it's not any better. :(

wysota
9th November 2010, 12:18
Use an additional QList to keep the order of insertions. Or use QCache :)

Vadim
11th November 2010, 00:08
What do you mean by "oldest item" ?
Do you want to remove the first item that was added to the map ? If so a map is not a good choice for that, as it does not track the order of the elements, and in fact the order is random and can change any time.
You can use a vector instead or in addition to the map. When adding a pointer to the map also add it to the vector. Then you would take the last element of the vector, find that element in the map and remove both the element form the map and from the vector.
It is a double penalty in terms of memory and time but I am afraid there is no way around that ( unless you do not need the map at all ).