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 - 自定义哈希函数的更多相关文章

  1. [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 ...

  2. Python 散列表查询_进入<哈希函数>为结界的世界

    1. 前言 哈希表或称为散列表,是一种常见的.使用频率非常高的数据存储方案. 哈希表属于抽象数据结构,需要开发者按哈希表数据结构的存储要求进行 API 定制,对于大部分高级语言而言,都会提供已经实现好 ...

  3. 我的MYSQL学习心得(十) 自定义存储过程和函数

    我的MYSQL学习心得(十) 自定义存储过程和函数 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心 ...

  4. 字符串哈希函数(String Hash Functions)

    哈希函数举例 http://www.cse.yorku.ca/~oz/hash.html Node.js使用的哈希函数 https://www.npmjs.org/package/string-has ...

  5. PHP 错误与异常 笔记与总结(8)自定义错误处理函数 set_error_handler()

    通过 Set_error_handler() 函数设置用户自定义的错误处理函数. 步骤: ① 创建错误处理函数 ② 设置不同级别调用函数 ③ Set_error_handler() 函数制定接管错误处 ...

  6. lintcode:哈希函数

    题目: 哈希函数 在数据结构中,哈希函数是用来将一个字符串(或任何其他类型)转化为小于哈希表大小且大于等于零的整数.一个好的哈希函数可以尽可能少地产生冲突.一种广泛使用的哈希函数算法是使用数值33,假 ...

  7. 在String()构造器不存在的情况下自定义一个MyString()函数,实现如下内建String()方法和属性:

    在String()构造器不存在的情况下自定义一个MyString()函数,实现如下内建String()方法和属性: var s = new MyString("hello"); s ...

  8. Eight(bfs+全排列的哈希函数)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22207   Accepted: 9846   Special Judge ...

  9. Qt自定义sleep延时函数(巧妙的使用时间差,但这样似乎CPU满格,而不是沉睡)

    Qt不像VC++的win32/MFC编程那样,提供了现成的sleep函数可供调用.Qt把sleep函数封装在QThread类中.子线程可以调用sleep函数.但是如果用户想在主线程实现延时功能,该怎么 ...

随机推荐

  1. 【2017多校训练08 1002】【HDOJ 6134】Battlestation Operational

    典型的数列反演题. 运用莫比乌斯反演的一个结论 $[n = 1] = \sum_{d | n} \mu(d)$,将表达式做如下转化: $$ ans = \sum_{i=1}^n \sum_{j=1}^ ...

  2. bzoj 3473 后缀自动机多字符串的子串处理方法

    后缀自动机处理多字符串字串相关问题. 首先,和后缀数组一样,用分割符连接各字符串,然后建一个后缀自动机. 我们定义一个节点代表的字符串为它原本代表的所有串去除包含分割符后的串.每个节点代表的字符串的数 ...

  3. Atlas 安装运行随笔

    Atlas 是一个用于数据库负载均衡 ,读写分离的中间件,他实现了mysql 的语法,对于普通调用DAL 层来说的话,和mysql 是一样的. 一,安装1,从源码安装 , 可以参考 http://bl ...

  4. jProfiler远程连接Linux监控jvm的运行状态

    第一步:下载软件官网地址:https://www.ej-technologies.com/download/jprofiler/files,下载一个linux服务端,一个windows客户端 GUI界 ...

  5. LogStash日志分析系统

    简介 通常日志管理是逐渐崩溃的——当日志对于人们最重要的时候,也就是出现问题的时候,这个渐进的过程就开始了.日志管理一般会经历一下3个阶段: 初级管理员将通过一些传统工具(如cat.tail.sed. ...

  6. Tomcat篇

    安装tomcat 先从tomcat官网找到最新的版本下载地址,我找的是Core下的安装包,下载到本地: wget http://mirror.bit.edu.cn/apache/tomcat/tomc ...

  7. MultCloud – 支持数据互传的网盘管理

    MultCloud https://www.multcloud.com/ 是一款在线服务,可以在一个地方管理众多网盘,支持国产百度盘, 最具有特色的地方是你可以直接在 MultCloud 里操作将 D ...

  8. LeetCode152:Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  9. C# 中的动态创建技术

    [转载]原文出处  http://blog.csdn.net/baiyun789/article/details/6156694 第一部分 WinForm控件在窗体中动态居中创建.删除控件及对其赋值 ...

  10. 由于拷贝的文件太大,不可能一直开着SHELL,所以让SCP后台运行

    原文地址: http://blog.itpub.net/90618/viewspace-750822/ 1:开一个终端,scp命令运行后,输入密码让其拷贝 # scp chris@221.179.1. ...