E
evlong
My question is about the STL map class and how it sorts data that it
stores. By default (as far as I know) it sorts keys in ascending order
or lexicographically.
If I were to do something like this:
ages["z"] = 1;
ages["a"] = 5;
ages["x"] = 1;
for( map<string, int>::iterator iter = ages.begin(); iter !=
ages.end(); iter++ ) {
cout << iter->first << " is " << (*iter).second << endl;
}
It would output
a is 5
x is 1
z is 1
Is there a way to tell map not to sort by keys or by value but to leave
them in the order in which they were created in the list? Or is there a
way to iterate through a map in the same order they were added to the
map?
Thanks in advance.
stores. By default (as far as I know) it sorts keys in ascending order
or lexicographically.
If I were to do something like this:
ages["z"] = 1;
ages["a"] = 5;
ages["x"] = 1;
for( map<string, int>::iterator iter = ages.begin(); iter !=
ages.end(); iter++ ) {
cout << iter->first << " is " << (*iter).second << endl;
}
It would output
a is 5
x is 1
z is 1
Is there a way to tell map not to sort by keys or by value but to leave
them in the order in which they were created in the list? Or is there a
way to iterate through a map in the same order they were added to the
map?
Thanks in advance.