C++ STL中允许重复key的multimap
在实际的项目中可能会碰到key重复的情况,正常的MAP类型是不允许重复的key,所以就要使用multimap了,multimap的使用和map基本类似,可以无缝对接
#include <map>
typedef pair<string, int> PAIR;
ostream& operator<<(ostream& out, const PAIR& p) {
return out << p.first << "\t" << p.second;
}
multimap<string, int> name_score_map;
//name_score_map["LiMin"] = 90; //主要插入的方式和map有所区别
name_score_map.insert(make_pair("LiMin", 90));
//name_score_map["LiMin"] = 91;
name_score_map.insert(make_pair("LiMin", 91));
//name_score_map["ZiLinMi"] = 79;
name_score_map.insert(make_pair("ZiLinMi", 79));
//name_score_map["BoB"] = 92;
name_score_map.insert(make_pair("BoB", 92));
name_score_map.insert(make_pair("Bing", 99));
for (multimap<string, int>::iterator iter = name_score_map.begin(); iter != name_score_map.end(); ++iter) {
cout << *iter << endl;
//cout << iter->first << " => " << iter->second << '\n';
}