#include <stdio.h>
#include <map>
using namespace std;
int main(){
// 声明map
map<int, int> mp;
for (int i = 0; i < 20; i++){
mp.insert(make_pair(i, i));
}
//删除key=0的元素
mp.erase(0);
//删除第一个元素
mp.erase(mp.begin());
//声明迭代器
map<int, int>::iterator it;
for (it = mp.begin(); it != mp.end(); it++){
//it->first指向key,it->second指向value
printf("%d->%d\n", it->first, it->second);
}
printf("-----------\n");
int count=mp.count(2);
printf("%d\n",count);
printf("-----------\n");
printf("-----------\n");
// 用key查找元素
it=mp.find(2);
mp.erase(it);
// 在末尾插入元素
mp.insert(make_pair(100,100));
mp.insert(pair<int,int>(101,101));
it=mp.end();
printf("%d->%d\n", it->first, it->second);
printf("-----------\n");
for (it = mp.begin(); it != mp.end(); it++){
printf("%d->%d\n", it->first, it->second);
}
return 0;
}
版权声明:本文为weixin_52719777原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。