#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
map<int,int>m;
int main()
{
m[1]=5,m[2]=4,m[3]=3,m[4]=2,m[5]=1;
//正向遍历
/*结果
1 5
2 4
3 3
4 2
5 1
*/
map<int,int>::iterator it;
for(it=m.begin();it!=m.end();it++){
cout << (*it).first << " " << (*it).second << endl;
}
//反向遍历
/*结果
5 1
4 2
3 3
2 4
1 5
*/
map<int,int>::reverse_iterator rit;
for(rit=m.rbegin();rit!=m.rend();rit++){
cout << (*rit).first << " " << (*rit).second << endl;
}
return 0;
}
c++中map的遍历
最新推荐文章于 2024-08-02 23:27:55 发布