









#include <iostream>
#include <map>
using namespace std;
/*
*map
*/
void printMap(map<int, int> &m) {
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
cout << it->first << " " << it->second << endl;
}
}
void test() {
map<int, int> m;
m.insert(make_pair(1, 1));
cout << m[1] << endl;
cout << m[2] << endl;
cout << endl;
printMap(m);
}
int main() {
test();
return 0;
}
1
0
1 1
2 0


本文介绍了一个简单的C++程序示例,展示了如何使用STL中的map容器进行键值对存储,并提供了插入元素及遍历打印的方法。

2万+

被折叠的 条评论
为什么被折叠?



