#include<iostream>
#include<map>
using namespace std;
int main()
{
map<int, string> mp;
//方法1.pair<int, string>
pair<map<int,string>::iterator, bool> pair1 = mp.insert(pair<int,string>(1,"11111"));
if (pair1.second == true)
{
cout<< "插入成功" <<endl;
}
else
{
cout<< "插入失败" <<endl;
}
pair<map<int,string>::iterator, bool> pair2 = mp.insert(pair<int,string>(1,"22222"));
if (pair2.second == true)
{
cout<< "插入成功" <<endl;
}
else
{
cout<< "插入失败" <<endl;
}
//方法2.make_pair
mp.insert(make_pair<int,string>(3,"33333"));
mp.insert(make_pair<int,string>(4,"44444"));
//方法3.value_type
mp.insert(map<int, string>::value_type(5,"55555"));
mp.insert(map<int, string>::value_type(6,"66666"));
//方法4.[]
mp[7] = "77777";
mp[7] = "88888";
for (map<int,string>::iterator it = mp.begin(); it != mp.end(); it++)
{
cout<< it->first << "\t" << it->second <<endl;
}
cout<< "--------------------------------" <<endl;
//删除
while(!mp.empty())
{
map<int,string>::iterator it = mp.begin();
cout<< it->first << "\t" << it->second <<endl;
mp.erase(it);
}
return 0;
}
C++ STL map4中不同的insert
最新推荐文章于 2021-11-26 12:06:58 发布