【C++】关联式容器multimap

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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值