使用string做输入比较好。
#include<map>
map<string, int> months;
months["may"] = 31;
string a="David D.";
months[a]=1984;
//months.insert(std::pair<string, int>(a, 1984));
string b="David D.";
printf("%d",months[b]) ;
//或者char*也可以
months.erase("David D.");
// 删除
打印map
// Map以从小到大的顺序排列: 具体表现位 0-9 A-Z a-z
map<char,int>m;
m['a']=1;
m['b']=2;
for(map<char,int>::iterator it=m.begin();it!=m.end();it++){
printf("%c %d\n",it->first,it->second);
}
判断某个键值是否存在:
m.count(key)==0
// 不存在
m.count(key)==1
// 存在
网友评论