C++ map遍历
C++ map遍历
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, int> _map;
_map[0] = 1;
_map[1] = 2;
_map[10] = 10;
map<int, int>::iterator iter;
iter = _map.begin();
while(iter != _map.end()) {
cout << iter->first << " : " << iter->second << endl;
iter++;
}
// 也可以使用for循环遍历
/*
for(iter = _map.begin(); iter != _map.end(); iter++) {
cout << iter->first << " : " << iter->second << endl;
}
*/
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
程序的运行结果为:
注意:
如果使用for循环遍历map,不能写成 ‘<’ 的形式