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函数.但是如果用户想在主线程实现延时功能,该怎么 ...
随机推荐
- Substring with Concatenation of All Words 题解
题意 You are given a string, s, and a list of words, words, that are all of the same length. Find all ...
- Elasticsearch中document的基础知识
写在前面的话:读书破万卷,编码如有神-------------------------------------------------------------------- 参考内容: <Ela ...
- 快速定位问题 Request无法获取参数
比如说最近开发甲修改了iframe标签的src,开发乙在设置src的时候传入了2个参数,通过iframe标签链接到这个页面时,开发乙调试时发现没有拿到任何参数值.然后开发乙百度了一下,发现iframe ...
- Apache 如何反向代理tomcat并且实现Session保持
简介 LAMT=Linux+Apache+MySQL+Tomcat: Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器: 在中小型系统和并发访问用户不是很多的场合下 ...
- ANSI、GBK、GB2312、UTF-8、GB18030和 UNICODE
http://www.chinaz.com/web/2012/1119/282540.shtml 编码一直是让新手头疼的问题,特别是 GBK.GB2312.UTF-8 这三个比较常见的网页编码的区别, ...
- MVC到底使用哪种方式传递Model,在ViewData、ViewBag、PartialView、TempData、ViewModel、Tuple之间取舍
在"MVC控制器传递多个Model到视图,使用ViewData, ViewBag, 部分视图, TempData, ViewModel, Tuple"中,体验了使用不同的方式传递多 ...
- 关于DirectShow SDK 和Windows SDK,及DirectX SDK
关于DirectShow SDK 和Windows SDK,及DirectX SDK 本文描述了DirectShow SDK ,Windows SDK,DirectX SDK ,VS200?之间的 ...
- 每天进步一点点——论fork()函数与Linux中的多线程编程
转载请说明出处:http://blog.csdn.net/cywosp/article/details/27316803 一.fork()函数 在操作系统的基本概念中进程是程序的一次运行,且是 ...
- 把NDK的工具链提取出来单独使用
独立toolchain 把NDK压缩包解压到系统,如/mnt目录下,后在/mnt目录下建立文件夹my_ndk_toolchain,然后再/mnt目录下执行以下命令:/mnt/android-ndk-r ...
- mysql访问权限GRANT ALL PRIVILEGES ON,访问权限表
开启远程连接:2, 修改 Mysql-Server 用户配置mysql> USE mysql; -- 切换到 mysql DBDatabase changedmysql> SELECT U ...