自定义类型使用hash_map时自定义hash函数
来源:互联网 发布:linux http proxy 编辑:程序博客网 时间:2024/06/04 01:14
#include <hash_map>
#include <string>
#include <iostream>
using namespace std;
//define the class
class ClassA{
public:
ClassA(int a):c_a(a){}
int getvalue()const { return c_a;}
void setvalue(int a){c_a;}
private:
int c_a;
};
//1 define the hash function
struct hash_A{
size_t operator()(const class ClassA & A)const{
// return hash<int>(classA.getvalue());
return A.getvalue();
}
};
//2 define the equal function
struct equal_A{
bool operator()(const class ClassA & a1, const class ClassA & a2)const{
return a1.getvalue() == a2.getvalue();
}
};
//可以在一个struct中同时定义两个函数
{
// 1. define the hash function
size_t operator()(const string& str) const
{
unsigned long __h = 0;
for (size_t i = 0 ; i < str.size() ; i ++)
__h = 5*__h + str[i];
return size_t(__h);
}
// 2. define the equal function
bool operator()(const string& p1, const string& p2) const{
return p1 == p2;
}
};
int main()
{
__gnu_cxx::hash_map<string, string, hash_string> strHMap;
__gnu_cxx::hash_map<ClassA, string, hash_A, equal_A> hmap;
ClassA a1(12);
hmap[a1]="I am 12";
ClassA a2(198877);
hmap[a2]="I am 198877";
strHMap["sss"] = "dddd";
cout<<hmap[a1]<<endl;
cout<<hmap[a2]<<endl;
return 0;
}
0 0
- 自定义类型使用hash_map时自定义hash函数
- hash_map自定义key类型的使用
- STL中hash_map深度剖析—使用自定义类型
- 【hash_map】hash_map中键为自定义类型的操作
- hash_map的key为自定义类型
- c++中关于hash_map自定义equal function和hash function
- hash_map的hash函数
- hash_map自定义key
- HIVE自定义函数类型
- 自定义类型转换函数
- Oracle自定义类型使用
- hash_map自定义数据类型作key
- hash_map 自实现hash函数容器错误
- 自定义判断客户端类型函数
- 使用VS2012 MFC自定义消息时,处理函数不能返回void类型。
- C++中对hash_map自定义哈希函数和比较函数的理解
- gdb 自定义函数使用
- smarty3使用自定义函数
- 由于事务隔离,查询不到最新的数据
- 入职第一天
- java流总结(1) 字节流
- 数据库的设计
- 欢迎使用CSDN-markdown编辑器
- 自定义类型使用hash_map时自定义hash函数
- Java内存对象及区域
- 数据库真的成为瓶颈了
- Java中访问权限的等级
- 进程上下文VS中断上下文
- lintcode之 合并排序数组 II
- ffmpeg中的pcm格式
- 数论--Lucas Theorem
- 关于Curator学习过程问题