hash_map自定义key类型的使用

// exam1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <hash_map>
#include <cv.h>
#include <iostream>
using namespace std;

struct pt_hash
{
	static const size_t   bucket_size = 4;
	static const size_t   min_buckets = 8;
	//哈希值函数
	size_t operator()(const CvPoint2D32f &pt)const
	{
		return pt.x*2+pt.y;
	}
	//小于号比较函数
	bool operator()(const CvPoint2D32f &pt1,const CvPoint2D32f &pt2)const
	{
		if(pt1.x<pt2.x)
			return true;
		else if(pt1.x>pt2.x)
			return false;
		else
		{
			return pt1.y<pt2.y;
		}
	}
};

int main()
{
	hash_multimap<CvPoint2D32f,string,pt_hash> map;
	pair<CvPoint2D32f,string> p;
	CvPoint2D32f pt;

	pt.x=100.12;
	pt.y=200.23;
	p.first=pt;
	p.second="cjc";
	map.insert(p);

	pt.x=300;
	pt.y=400;
	p.first=pt;
	p.second="cxc";
	map.insert(p);

	pt.x=100.12;
	pt.y=200.23;
	p.first=pt;
	p.second="cxc";
	map.insert(p);

	cout<<map.size()<<endl;

	hash_multimap<CvPoint2D32f,string,pt_hash>::iterator be=map.lower_bound(pt),
		ed=map.upper_bound(pt),it;
	for(it=be;it!=ed;it++)
	{
		cout<<it->second<<endl;
	}

	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值