multimap是关联式容器,它按照特定的顺序存储由key和value映射成的键值对<key,value>,其中多个键值之间的key可以重复。
总结:
multimap的key可以重复;
multimap中的元素默认将key按照小于来比较;
multimap没有重载operator[];
multimap的头文件与map相同。
插入相同key值可重复
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main()
{
multimap<string, string> m;
m.insert(make_pair("李娜", "Lina"));
m.insert(make_pair("玛丽", "Mary"));
m.insert(make_pair("汤姆", "Tom"));
m.insert(make_pair("玛丽", "Marry"));
cout << m.size() << endl;
for (auto& e : m)
{
cout << e.first << "--->" << e.second << endl;
}
cout << m.count("玛丽") << endl;
system("pause");
return 0;
}
没有operator[],可使用范围for遍历。
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main()
{
multimap<int,int> mr;
for (int i = 0; i < 10; ++i)
{
mr.insert(pair<int, int>(i, i));
}
for (auto& e : mr)
{
cout << e.first << "--->" << e.second << endl;
}
cout << endl;
//大于5的第一个元素
auto it = mr.lower_bound(5);
cout << it->first << "--->" << it->second << endl;
//返回大于5的元素
it = mr.upper_bound(5);
cout << it->first << "--->" << it->second << endl;
system("pause");
return 0;
}