Link to home
Start Free TrialLog in
Avatar of duncanlatimer
duncanlatimer

asked on

looking for something like a std::map

I am looking for a stl class which is like a std::map, but which doesn't sort the elements, i.e. like a vector of pairs, but where you can do a find like you can with a std::map. Obviously I am aware that I could write a class that is a vector of pairs with a find method, however there is probably a stl class that does exactly what I want already.
Avatar of Axter
Axter
Flag of United States of America image

You can try using a hash_map class.
hash_map is not part of the official C++ standards, but you can download a free copy of the class if you're intererested.
Avatar of jasonclarke
jasonclarke

I don't think there is such a thing.

Maybe multimap is what you want, it allows you to have multiple values with the same key.  It is sorted, however.

If sorting is a problem you could also try the hash associative container that is part of the SGI (and STLport) implementation of the STL.

See: http://www.sgi.com/tech/stl/HashedAssociativeContainer.html

for details and www.stlport.org for the stlport download.

However, you should note that the entire point of these associative containers is to provide fast access by key value - so the classes need some way to store key values for efficient lookup.
Didn't see your comment there Axter.
Avatar of duncanlatimer

ASKER

As the order of the elemets in a hash_map are not guaranteed this will probably not work for me. I need to be able to retrieve the elements in the same order that I loaded them. And I also need to be able to do a find on a key.
I envisage that the container I am after has two different types of iterators, one type providing sorted access for finds etc, the other providing load sequence order access.
The solution may well depend then on what your main concern is.  If speed of lookup is very important then you might consider storing maybe a vector and a map, maybe they could contain pointers to stored data structures, or maybe the vector could just contain key values.

i.e. something like:

vector<string> keyVector;
map<string,value>  mymap;

then you could implement a 'get by index' something like this:

value getByIndex(size_t index) const
{
    return mymap[keyVector[index]];
}

(you might want to change it add some error checking!).
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
MFC has a CMap class
I am trying to avoid MFC and Axter's solution is exactly what I need so...