map

作者: 小幸运Q | 来源:发表于2018-08-15 19:29 被阅读9次

使用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
// 存在

相关文章

网友评论

      本文标题:map

      本文链接:https://www.haomeiwen.com/subject/htxqjftx.html