插入:
m.insert(map<int,string>::value_type(1,"a"));
修改:
m.insert(map<int,string>::value_type(1,"b"));
m.insert(make_pair<int,string>(1,"b"));
m[1] = "b";
for(map<int,string>::iterator i = m.begin() ; i != m.end() ; i++){
if(i->first == 2)
i->second = "what";
}
删除:
m[1] = "a";
m[2] = "b";
m[3] = "c";
for(map<int,string>::iterator i = m.begin() ; i != m.end() ; i++){
if(i->first == 2)
m.erase(i);
}
完整代码:
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(){
map<int,string> m;
m.insert(map<int,string>::value_type(1,"a"));
m.insert(map<int,string>::value_type(1,"b"));
m.insert(make_pair<int,string>(1,"b"));
m[1] = "a";
m[2] = "b";
m[3] = "c";
for(map<int,string>::iterator i = m.begin() ; i != m.end() ; i++){
if(i->first == 2)
m.erase(i);
}
for(map<int,string>::iterator i = m.begin() ; i != m.end() ; i++)
cout<<i->second;
return 0;
}
补充:
if(M.count(key))
cout<<"已有"<<endl;
else
cout<<"没有"<<endl;