multimap用法

#include <stdio.h>
#include <map>
#include <xtree>
#include <algorithm>
using namespace std;

int main(int argc, char** argv)
{
	multimap<int, int> mmap;
	mmap.insert(pair<int, int>(1, 1));
	mmap.insert(pair<int, int>(1, 2));
	mmap.insert(pair<int, int>(2, 3));
	multimap<int, int>::iterator iter = mmap.begin();
	for (; iter != mmap.end(); ++iter)
		printf("%d, %d\n", iter->first, iter->second);
	
	//查找指定key的元素方法一:
	multimap<int, int>::iterator iter_lower_bound = mmap.lower_bound(1);
	multimap<int, int>::iterator iter_upper_bound = mmap.upper_bound(1);
	for (; iter_lower_bound != iter_upper_bound; iter_lower_bound++)
		printf("%d, %d\n", iter_lower_bound->first, iter_lower_bound->second);
	
	//查找指定key的元素方法二:
	iter = mmap.find(1);
	int num = mmap.count(1);
	int k = 0;
	for (; iter != mmap.end() && k < num; iter++, k++)
		printf("%d, %d\n", iter->first, iter->second);
	
	
	//查找指定key的元素方法三:
	pair<multimap<int, int>::iterator, multimap<int, int>::iterator> p_ret = mmap.equal_range(1);
	while (p_ret.first != p_ret.second)
	{
		printf("%d, %d\n", p_ret.first->first, p_ret.first->second);
		p_ret.first++;
	}

	getchar();
	return 0;
}

运行结果为:

1, 1
1, 2
2, 3
1, 1
1, 2
1, 1
1, 2
1, 1
1, 2



如果键是降序排列的话

#include <stdio.h>
#include <map>
#include <xtree>
#include <algorithm>
using namespace std;

struct GreaterComp : public binary_function<const int&, const int&, bool>
{
	result_type operator()(first_argument_type _Left, second_argument_type& _Right)
	{
		return _Left > _Right;
	}
};

int main(int argc, char** argv)
{
	multimap<int, int, GreaterComp> mmap;
	mmap.insert(pair<int, int>(4, 3));
	mmap.insert(pair<int, int>(4, 3));
	mmap.insert(pair<int, int>(5, 3));
	mmap.insert(pair<int, int>(2, 3));
	mmap.insert(pair<int, int>(1, 1));
	mmap.insert(pair<int, int>(1, 2));
	mmap.insert(pair<int, int>(1, 5));
	mmap.insert(pair<int, int>(2, 3));
	multimap<int, int, GreaterComp>::iterator iter = mmap.begin();
	for (; iter != mmap.end(); ++iter)
		printf("%d, %d\n", iter->first, iter->second);
	printf("\n");

	//查找指定key的元素方法一:
	multimap<int, int, GreaterComp>::iterator iter_lower_bound = mmap.lower_bound(1);
	multimap<int, int, GreaterComp>::iterator iter_upper_bound = mmap.upper_bound(1);
	for (; iter_lower_bound != iter_upper_bound; iter_lower_bound++)
		printf("%d, %d\n", iter_lower_bound->first, iter_lower_bound->second);
	printf("\n");

	//查找指定key的元素方法二:
	iter = mmap.find(1);
	int num = mmap.count(1);
	int k = 0;
	for (; iter != mmap.end() && k < num; iter++, k++)
		printf("%d, %d\n", iter->first, iter->second);
	printf("\n");
	
	//查找指定key的元素方法三:
	pair<multimap<int, int, GreaterComp>::iterator, multimap<int, int, GreaterComp>::iterator> p_ret = mmap.equal_range(1);
	while (p_ret.first != p_ret.second)
	{
		printf("%d, %d\n", p_ret.first->first, p_ret.first->second);
		p_ret.first++;
	}
	printf("\n");

	getchar();
	return 0;
}

运行结果为

5, 3
4, 3
4, 3
2, 3
2, 3
1, 1
1, 2
1, 5

1, 1
1, 2
1, 5

1, 1
1, 2
1, 5

1, 1
1, 2
1, 5

可以发现,操作是没什么不同的

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值