STL, hash_map is sorted !!

Mohammad Shojatalab shoja@ebi.ac.uk
Mon Jun 26 13:25:00 GMT 2006


Hi  There,

I am trying to make use of hash_map from STL, and I was assuming that 
hash_map doesn't sort the (key, value) pairs and keep the
pairs in the order I inserted them into the hash_map.

This is the simple program:

#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

typedef vector<int> cvCodes;
typedef hash_multimap<int, std::string> cvValues;

int main()
{
 cvCodes items;
 cvValues itemsvalues;
 cvValues::iterator pos;

 itemsvalues.insert(make_pair(3,"Z"));
 itemsvalues.insert(make_pair(1,"C"));
 itemsvalues.insert(make_pair(2,"A"));

 for (pos = itemsvalues.begin(); pos != itemsvalues.end(); pos++)
   cout << pos->first << "  " << pos->second << endl;

return 0;
}

I was expecting to see the following output:

3  Z
1  C
2  A

but instead I get this one:

1  C
2  A
3  Z

I need to use a container which keeps the order of thing as I insert them.

Im using gcc version 4.0.2 20051125 (Red Hat 4.0.2-8)
on Linux   2.6.14-1.1653_FC4smp

I appreciate any help.

Thanks
Mohammad




More information about the Gcc-help mailing list