[Qt-interest] New container class needed in Qt

Mandeep Sandhu mandeepsandhu.chd at gmail.com
Tue Oct 18 07:34:03 CEST 2011


Hi All,

I have a requirement where I want a Map/Hash like container on which I
can do key based lookups but also have the items stored in the order
in which they were inserted.

To elaborate, I'll detail my requirement with a simple example.

Consider that we have received a list of items from some external
source. The list contains strings in a particular order, eg: ["D",
"B", "A" , "C"].

I want to however, store some associated values along with each item
of the list. eg:
A - "Apple"
B - "Ball"
C - "Cat"
D - "Donkey" :)

I want to store this in a dictionary like structure because I would
want to later 'lookup' the associated values with a given key
(A/B/C..).

If I simply store it in a QMap, I can do lookups, but then the items
would get sorted alphabetically (A,B,C,D). However I want to preserve
the order of the keys. So using a QHash or QMap won't work.

A way of achieving the require behavior is to use a List, so that the
order of keys is preserved. But then there's no easy way to lookup any
arbitrary value (apart from iterating the list).
I don't want to maintain 2 structures (a list for keeping track of key
order and a map for value lookup).

Any suggestions about how existing container classes can be used to
achieve this?

Thanks,
-mandeep



More information about the Qt-interest-old mailing list