如何逆向遍历map——C++新手上机疑难点总结⑨
1. 正向遍历map:
map<int, int> mymap;
map<int, int>::iterator it;
for(it=mymap.begin();it!=mymap.end();it++){
cout << it->first << " " << it->second << endl;
}
2. 逆向遍历map:
map<int, int> mymap;
map<int, int>::reverse_iterator it;
for(it=mymap.rbegin();it!=mymap.rend();it++){
cout << it->first << " " << it->second << endl;
}