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<<6) + (seed>>2);
}// 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 = 0;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 criterionunordered_set<Customer, CustomerHash, CustomerEqual> custset;custset.insert(Customer("arne", "wink", 70));custset.insert(Customer("peter", "zhang", 70));PRINT_ELEMENTS(custset);Customer cust = Customer("arne", "wink", 70);if (custset.find(cust) != custset.end()){cout << "Customer: " << cust << " found!" << endl;}else{cout << "Customer: " << cust << " not exists!" << endl;}Customer cust2 = Customer("arne", "wink2", 70);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. mysql 自定义哈希函数_C++ STL无序容器自定义哈希函数和比较规则(超级详细)...

    前面在讲解 unordered_map.unordered_multimap.unordered_set 以及 unordered_multiset 这 4 种无序关联式容器(哈希容器)时,遗留过一个 ...

  2. 高级数据结构与算法 | 哈希 :哈希冲突、负载因子、哈希函数、哈希表、哈希桶

    文章目录 哈希 哈希函数 常见的哈希函数 字符串哈希函数 哈希冲突 闭散列的解决方法 开散列的解决方法 负载因子以及增容 对于闭散列 对于开散列结构 具体实现 哈希表(闭散列) 插入 查找 删除 完整 ...

  3. 哈希 :哈希冲突、负载因子、哈希函数、哈希表、哈希桶

    文章目录 哈希 哈希(散列)函数 常见的哈希函数 字符串哈希函数 哈希冲突 闭散列(开放地址法) 开散列(链地址法/拉链法) 负载因子以及增容 对于闭散列 对于开散列结构 具体实现 哈希表(闭散列) ...

  4. LeetCode 1263. 推箱子(BFS+DFS / 自定义哈希set)

    文章目录 1. 题目 2. 解题 2.1 超时解 2.2 BFS + DFS 1. 题目 「推箱子」是一款风靡全球的益智小游戏,玩家需要将箱子推到仓库中的目标位置. 游戏地图用大小为 n * m 的网 ...

  5. 里奥老师乾坤大挪移—哈希表与哈希函数专题1

    首先还是要先感谢左神 11.10.基础提升 哈希函数与哈希表等 1.要想掌握这么经典且复杂的问题,祭出埃隆马斯克的第一性原理+乾坤大挪移:表就是映射.函数就是映射. 2.说到哈希表,实在是太经典的数据 ...

  6. 散列函数(哈希函数,Hash Function)

    说明 散列的概念属于查找,它不以关键字的比较为基本操作,采用直接寻址技术.在理想情况下,查找的期望时间为O(1). 简单的说,hash函数就是把任意长的输入字符串变化成固定长的输出字符串的一种函数.输 ...

  7. C++ 哈希表查询_进入哈希函数结界的世界

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

  8. 哈希函数(Hash Function)

    哈希函数(散列函数) 说明 应用 解释 Q:冲突是不是可以避免的? hash函数的构造准则:简单.均匀 hash函数的构造方法: 处理冲突的方法: 参考 说明 散列的概念属于查找,它不以关键字的比较为 ...

  9. 她破解哈希函数算法:坚持10年做一件事一定能做成

    今年9月7日,2019未来科学大奖揭晓,这项设立4年的大奖迎来首位女性得主--密码学家王小云.在信息时代,金融服务.网络安全等背后都离不开密码系统的"护航".在大众眼中,密码带着神 ...

最新文章

  1. 培育强人工智能的「ImageNet」:上海交大卢策吾组提出铰接物体知识库 AKB-48
  2. 有点酸!中外程序员都是怎么炫富的? | 每日趣闻
  3. VR/AR标准委员会成立OpenXR工作组:Oculus、Valve领头
  4. js动态改变下拉菜单内容示例 .
  5. Spring Boot Admin:微服务应用监控
  6. php数组地址,怎么实现javascript数组与php数组的地址传递
  7. esclip直接快捷键构造函数_史上最全IntelliJ IDEA mac版快捷键文档
  8. ASP.NET视图状态解析(本博客仅是自己留着作为存储学习)---选自MSDN
  9. mysql 常用命令集_Mysql 常用命令集
  10. 利用gensim在simulink中建立神经网络模型
  11. 基于ARP协议获取局域网内主机MAC地址
  12. OSPF邻接关系建立
  13. 研发管理和项目管理有哪些痛点?
  14. 易语言pc微信hook最新版本
  15. Python中的Bunch模式
  16. js过滤检测敏感词汇
  17. 【未雨绸缪】海南测控天线系统防御台风的措施
  18. #CF div2 1670D Very Suspicious题解(思维+贪心)
  19. Python利用tkinter制作桌面翻译小工具
  20. 设置windows10相应文件格式的打开方式

热门文章

  1. cheerio的小案例
  2. Linux批量部署 EXPECT 使用
  3. Ruby BigDecimal库拒绝服务漏洞
  4. LINUX 数据盘的格式化与挂载
  5. HttpWebResponse 的问题
  6. Python module模块 包 __name__
  7. 微信小程序_(表单组件)button组件的使用
  8. luoguP5035金坷垃
  9. 数据结构:哈希表(散列表)基础
  10. 深入浅出学习Hibernate框架(一):从实例入手初识Hibernate框架