map<int, string> MyMap;
//下标方式 key值重复进行替换
MyMap[0] = "233";
MyMap[0] = "23333";
//insert方法 key值重复无法插入
MyMap.insert(pair<int, string>(1, "zhangsan"));
MyMap.insert(pair<int, string>(1, "zhangsan2"));
MyMap.insert(map<int, string>::value_type(2, "666"));
//遍历
map<int, string>::iterator iter;
for (iter = MyMap.begin(); iter != MyMap.end(); iter++) {
	cout << iter->first << " - " << iter->second << endl;
}
cout << endl;
//新遍历
for (auto iter2 : MyMap) {
	cout << iter2.first << " - " << iter2.second << endl;
}
cout << endl;
//是否插入成功
pair<map<int, string>::iterator, bool> MyPair;
MyPair = MyMap.insert(map<int, string>::value_type(2, "6667"));
cout << boolalpha << MyPair.second << endl;
cout << endl;
//查找
map<int,string>::iterator iter3 = MyMap.find(1);
if (iter3 != MyMap.end()) {
	cout << iter3->first << " - " << iter3->second << endl;
}
cout << endl;
//删除
MyMap.erase(iter3);
MyMap.erase(2);
for (iter = MyMap.begin(); iter != MyMap.end(); iter++) {
	cout << iter->first << " - " << iter->second << endl;
}
posted on 2024-02-18 13:52  baihuntun  阅读(10)  评论(0)    收藏  举报