std::map key sorting
Aug 27, 2014 at 6:54am UTC
Hi,
I'm wondering if there is an equivalent of std::map in which the iterator is sorted in the way it was inserted without going through its default sorting? For instance,
1 2 3 4 5 6 7
map<long , string> my_map;
my_map[1] = "a" ;
my_map[10] = "b" ;
my_map[2] = "c" ;
for (map<long , string>::const_iterator it = my_map.begin(); it != my_map.end(); it++) {
cout << it->first << ", " << it->second << endl;
}
will give
1, a
2, c
10, b
However what I want is
1, a
10, b
2, c
in the same order as it was originally inserted. Is there a way to override this default map sorting?
Aug 27, 2014 at 7:51am UTC
You can give the std::map third template parameter: Compare
That way you dictate the ordering.
Perhaps vector<pair<long ,string>>
works too. Depends on how you use the my_map.
Topic archived. No new replies allowed.