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函数.但是如果用户想在主线程实现延时功能,该怎么 ...
随机推荐
- SQL SERVER 2008 多边形问题的解决
报错内容: 在执行用户定义例程或聚合 "geometry" 期间出现 .NET Framework 错误: System.ArgumentException: 24144: 由于该 ...
- Uva 12889 One-Two-Three
Your little brother has just learnt to write one, two and three, in English. He has written a lot ...
- [Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. See https://www.chromestatus.com/features/5093566007214080
相信如果用谷歌浏览器做移动端页面的时候 用touch事件的时候应该遇到过这个东东吧 documet.addEventListener("touchstart",function() ...
- RAD Studio 2010~XE8 官方 ISO 下载地址 (2015-03-28更新)
http://bbs.csdn.net/topics/390816856 RAD Studio XE8 目前最新版 v22.0.19027.8951 官方 ISO 文件下载(6.72GB):http: ...
- MYSQL 慢日志
http://blog.chinaunix.net/uid-9950859-id-122259.html
- 快速安装自己的Sublime系列
GitHub:http://liu12fei08fei.github.io/html/2sublime.html 安装插件管理(Package Control): Sublime Text 支持大量插 ...
- js单例模式详解实例
这篇文章主要介绍了什么是单例单例模式.使用场景,提供了3个示例给大家参考 什么是单例? 单例要求一个类有且只有一个实例,提供一个全局的访问点.因此它要绕过常规的控制器,使其只能有一个实例,供使用者使用 ...
- C#程序集系列08,设置程序集版本
区别一个程序集,不仅仅是程序集名称,还包括程序集版本.程序集公匙.程序集文化等,本篇体验通过界面和编码设置程序集版本. □ 通过Visual Studio设置程序集版本 →右键项目,选择"属 ...
- [转载] MATLAB快捷键
原文地址,点此查看 一.常用对象操作 除了一般windows窗口的常用功能键外. 1.!dir 可以查看当前工作目录的文件. !dir& 可以在dos状态下查看. 2.who 可以查看当前 ...
- 在Windows Phone项目中调用C语言DLL
在Windows Phone项目中调用C语言写的DLL 最近接到一个需求,需要在WP里调用一个C语言写的DLL,并且说Android和iOS都可以,问我WP是否可以这样? 我说我调研一下,就有了下面的 ...