How does map iterator work?
Jun 16, 2017 at 5:23am UTC
I have seen a standard example of iterator on this website, when I compiled this program the output was in alphabetical order.
I tried the same program with a map of map<int,int> type, again it arranged the output in ascending order.
can anybody tell me how does this work ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// map::begin/end
#include <iostream>
#include <map>
int main ()
{
std::map<char ,int > mymap;
mymap['b' ] = 100;
mymap['a' ] = 200;
mymap['c' ] = 300;
// show content:
for (std::map<char ,int >::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n' ;
return 0;
}
Jun 16, 2017 at 5:36am UTC
<maps> automatically store values in sort-order.
Internally, the elements in a map are always sorted by its key following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare).
See: http://www.cplusplus.com/reference/map/map/
Last edited on Jun 16, 2017 at 5:37am UTC
Jun 25, 2017 at 3:43pm UTC
Thanks
Topic archived. No new replies allowed.