STL - Unorderedset - 自定义哈希函数
1. hash工具类
hashval.hpp
#ifndef _Core_HashVal_H_
#define _Core_HashVal_H_ #include <functional> // from boost (functional/hash):
// see http://www.boost.org/doc/libs/1_35_0/doc/html/hash/combine.html
template <typename T>
inline void hash_combine (std::size_t& seed, const T& val)
{
seed ^= std::hash<T>()(val) + 0x9e3779b9 + (seed<<) + (seed>>);
} // auxiliary generic functions to create a hash value using a seed
template <typename T>
inline void hash_val (std::size_t& seed, const T& val)
{
hash_combine(seed,val);
}
template <typename T, typename... Types>
inline void hash_val (std::size_t& seed,
const T& val, const Types&... args)
{
hash_combine(seed,val);
hash_val(seed,args...);
} // auxiliary generic function to create a hash value out of a heterogeneous list of arguments
template <typename... Types>
inline std::size_t hash_val (const Types&... args)
{
std::size_t seed = ;
hash_val (seed, args...);
return seed;
} #endif
2. UnorderedSetTest.cpp
#include <unordered_set>
#include "../../Core/print.hpp"
#include "UnorderedSetTest.h"
#include "../../Core/hashval.hpp"
#include "../../Domain/Models/Customer.h"
#include "../../Domain/Models/CustomerHash.h"
#include "../../Domain/Models/CustomerEqual.h"
#include <string>
#include <iostream> using namespace std; void UnorderedSetTest::simpleHashFunc()
{
// unordered set with own hash function and equivalence criterion
unordered_set<Customer, CustomerHash, CustomerEqual> custset; custset.insert(Customer("arne", "wink", ));
custset.insert(Customer("peter", "zhang", ));
PRINT_ELEMENTS(custset); Customer cust = Customer("arne", "wink", );
if (custset.find(cust) != custset.end())
{
cout << "Customer: " << cust << " found!" << endl;
}
else
{
cout << "Customer: " << cust << " not exists!" << endl;
} Customer cust2 = Customer("arne", "wink2", );
if (custset.find(cust2) != custset.end())
{
cout << "Customer: " << cust2 << " found!" << endl;
}
else
{
cout << "Customer: " << cust2 << " not exists!" << endl;
}
} void UnorderedSetTest::run()
{
printStart("simpleHashFunc()");
simpleHashFunc();
printEnd("simpleHashFunc()");
}
3. 运行结果:
---------------- simpleHashFunc(): Run Start ----------------
[arne,wink,70] [peter,zhang,70]
Customer: [arne,wink,70] found!
Customer: [arne,wink2,70] not exists!
---------------- simpleHashFunc(): Run End ----------------
STL - Unorderedset - 自定义哈希函数的更多相关文章
- [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map
13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...
- Python 散列表查询_进入<哈希函数>为结界的世界
1. 前言 哈希表或称为散列表,是一种常见的.使用频率非常高的数据存储方案. 哈希表属于抽象数据结构,需要开发者按哈希表数据结构的存储要求进行 API 定制,对于大部分高级语言而言,都会提供已经实现好 ...
- 我的MYSQL学习心得(十) 自定义存储过程和函数
我的MYSQL学习心得(十) 自定义存储过程和函数 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心 ...
- 字符串哈希函数(String Hash Functions)
哈希函数举例 http://www.cse.yorku.ca/~oz/hash.html Node.js使用的哈希函数 https://www.npmjs.org/package/string-has ...
- PHP 错误与异常 笔记与总结(8)自定义错误处理函数 set_error_handler()
通过 Set_error_handler() 函数设置用户自定义的错误处理函数. 步骤: ① 创建错误处理函数 ② 设置不同级别调用函数 ③ Set_error_handler() 函数制定接管错误处 ...
- lintcode:哈希函数
题目: 哈希函数 在数据结构中,哈希函数是用来将一个字符串(或任何其他类型)转化为小于哈希表大小且大于等于零的整数.一个好的哈希函数可以尽可能少地产生冲突.一种广泛使用的哈希函数算法是使用数值33,假 ...
- 在String()构造器不存在的情况下自定义一个MyString()函数,实现如下内建String()方法和属性:
在String()构造器不存在的情况下自定义一个MyString()函数,实现如下内建String()方法和属性: var s = new MyString("hello"); s ...
- Eight(bfs+全排列的哈希函数)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22207 Accepted: 9846 Special Judge ...
- Qt自定义sleep延时函数(巧妙的使用时间差,但这样似乎CPU满格,而不是沉睡)
Qt不像VC++的win32/MFC编程那样,提供了现成的sleep函数可供调用.Qt把sleep函数封装在QThread类中.子线程可以调用sleep函数.但是如果用户想在主线程实现延时功能,该怎么 ...
随机推荐
- 2013年JavaScript开发人员调查结果
JavaScript开发人员调查现在已经结束,一如既往社区对结果进行了进一步分析: 总结(汉语) 原始数据(电子表格) 2012年结果 51%的被参与者写客户端代码,而28%的人说他们编写服务器端代码 ...
- gearman学习笔记
<?php 搭建描述: . 在实际使用时应该是运行gearmand -d 的 server 一台. [要装gearmand,运行gearmand] . 处理worker的机器若干[要装gearm ...
- VC 操作 EXCEL---插入工作表(Insert.Sheet)方法
看到的资料 http://bbs.csdn.net/topics/198565 自己总结一下 //插入到nIndex工作表之前 void InsertSheet(int nIndex) { sheet ...
- URAL 1880 Psych Up's Eigenvalues
1880. Psych Up's Eigenvalues Time limit: 0.5 secondMemory limit: 64 MB At one of the contests at the ...
- SpringMVC 方法参数设置
/** 在方法中配置参数: (1) 内置对象配置: request:获取cookie.请求头... 获取项目根路径 request.getContextPath() response:用于ajax的输 ...
- spring---aop(8)---Spring AOP中optimize
写在前面 optimize是ProxyConfig的属性.意思为 是否对生产代理策略使用优化. public class ProxyConfig implements Serializable { p ...
- 数据是企业的无价財富——爱数备份存储柜server的初体验(图文)
非常早就像上这样一套数据备份系统,每天採用原来的软件备份加手动备份的方式,总有些不是太方便的地方. 加上企业规模的不断扩大,系统的增多,业务数据也日显重要.容不得半点中断和数据丢失.这不,出于对系统数 ...
- WebSocket重连reconnecting-websocket.js的使用
原文:https://www.cnblogs.com/kennyliu/p/6477746.html 页面引用 <script src="~/Scripts/reconnectin ...
- xcode4.3.2 arc模式下导入非arc的文件 转
在arc模式下,我们经常会用到非arc的类库,此时我们可以在Compile Sources下对该文件进行编辑加入 -fno-objc-arc 如图中所示,就可以使用非arc的类库了 转:htt ...
- ExtJS创建选项卡
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...