C++ unordered_map/unordered_set 自定义键类型
1. unordered_map 和 unordered_set
template < class Key, // unordered_map::key_type
class T, // unordered_map::mapped_type
class Hash = hash<Key>, // unordered_map::hasher
class Pred = equal_to<Key>, // unordered_map::key_equal
class Alloc = allocator< pair<const Key,T> > // unordered_map::allocator_type
> class unordered_map;
template < class Key, // unordered_set::key_type/value_type
class Hash = hash<Key>, // unordered_set::hasher
class Pred = equal_to<Key>, // unordered_set::key_equal
class Alloc = allocator<Key> // unordered_set::allocator_type
> class unordered_set;
2. 方法1
struct Bytes {
size_t _nbytes;
const char* _data;
// key_equal
bool operator==(const Bytes& bytes) const {
if (_nbytes != bytes._nbytes) {
return false;
}
return memcmp(_data, bytes._data, _nbytes) == 0;
}
};
namespace std {
// hasher
template <>
struct hash<Bytes> {
// 与 string 的 hash 函数具有一致的结果
size_t operator()(const Bytes& bytes) const {
size_t value = 2166136261U;
size_t prime = 16777619U;
const char* data = bytes._data;
for (size_t i = 0; i < bytes._nbytes; i++) {
value ^= static_cast<size_t>(data[i]);
value *= prime;
}
return value;
}
};
};
3. 方法2
struct Bytes {
size_t _nbytes;
const char* _data;
};
// key_equal
bool bytes_key_equal(const Bytes& bs1, const Bytes& bs2) {
if (bs1._nbytes != bs2._nbytes) {
return false;
}
return memcmp(bs1._data, bs2._data, bs1._nbytes) == 0;
}
// hasher
size_t bytes_hash(const Bytes& bytes) {
size_t value = 2166136261U;
size_t prime = 16777619U;
const char* data = bytes._data;
for (size_t i = 0; i < bytes._nbytes; i++) {
value ^= static_cast<size_t>(data[i]);
value *= prime;
}
return value;
}
int main() {
size_t init_cap = 16;
unordered_map<Bytes, string, function<size_t(const Bytes&)>, function<bool(const Bytes&, const Bytes&)>> umap(init_cap, bytes_hash, bytes_key_equal);
Bytes bs;
bs._data = "hello";
bs._nbytes = 5;
umap[bs] = "world";
return 0;
}
参考资料
http://www.cplusplus.com/reference/unordered_set/unordered_set/
http://www.cplusplus.com/reference/unordered_map/
https://blog.csdn.net/y109y/article/details/82669620
C++ unordered_map/unordered_set 自定义键类型的更多相关文章
- C++ | unordered_map 自定义键类型
C++ unordered_map 使用自定义类作为键类型 C++ unordered_map using a custom class type as the key
- STL: unordered_map 自定义键值使用
使用Windows下 RECT 类型做unordered_map 键值 1. Hash 函数 计算自定义类型的hash值. struct hash_RECT { size_t operator()(c ...
- map自定义键值类型
map自定义键值类型 改变Map的默认比较方式 https://www.cnblogs.com/zjfdlut/archive/2011/08/12/2135698.html 大家知道,STL中的ma ...
- c/c++ 标准库 set 自定义关键字类型与比较函数
标准库 set 自定义关键字类型与比较函数 问题:哪些类型可以作为标准库set的关键字类型呢??? 答案: 1,任意类型,但是需要额外提供能够比较这种类型的比较函数. 2,这种类型实现了 < 操 ...
- c++ 标准库的各种容器(vector,deque,map,set,unordered_map,unordered_set,list)的性能考虑
转自:http://blog.csdn.net/truexf/article/details/17303263 一.vector vector采用一段连续的内存来存储其元素,向vector添加元素的时 ...
- Java用自定义的类型作为HashMap的key
需要重写hashCode()和equals()方法才可以实现自定义键在HashMap中的查找. public class PhoneNumber { private int prefix; //区 ...
- [C#] 类型学习笔记三:自定义值类型
既前两篇之后,这一篇我们讨论通过struct 关键字自定义值类型. 在第一篇已经讨论过值类型的优势,节省空间,不会触发Gargage Collection等等. 在对性能要求比较高的场景下,通过str ...
- wordpress添加post_type自定义文章类型
wordpress很强大,能当博客也能进行二次开发出很完善的内容管理系统满足企业运营需求,比如可以添加products产品模型.汽车模型等,如何实现呢?添加post_type自定义文章类型就可以了 p ...
- Python - Django - ORM 自定义 char 类型字段
用 CharField 定义的字段在数据库中存放为 verchar 类型 自定义 char 类型字段需要下面的代码: class FixedCharField(models.Field): " ...
随机推荐
- 基于Python实现环形队列高效定时器
定时器Python实现代码 import time import redis import multiprocessing class Base: """ redis配置 ...
- 关于新创公司所需的icp,网文,软著和备案的申请
刚从一个集团离职来到了创业团队,前期是什么都没有,甚至是公司名字都不知道,哈哈.所以就有了后面的坑踩了一遍又一遍.刚开始是在霍尔果斯注册,结果办icp费了半年的时间,东找西找还没下证.又碰上新疆严查不 ...
- webform中配置服务器控件的样式
前台 Style <asp:Label ID="Label1" runat="server" Text="Label" Style=& ...
- 5.装饰模式 Decorator (单一职责)
结合: Android设计模式 006 装饰者模式 [B站]对整个重构的细节讲的容易懂 Android的设计模式-装饰者模式 [简书]结合安卓源码讲的还可以,让我对context有更深入的理解 ...
- BP暴力破解
BurpSuite暴力破解 1.设置代理 首先要用phpstudy打开Mysql和Apache,然后将设置浏览器代理,地址127.0.0.1 端口8080 2.进入dvwa靶场 进入dvwa时,要用 ...
- AES 逻辑
分组长度 加密逻辑 轮函数 参考:链接 字节代换 两种方法: 1.首先(将字节看做GF(28)上的元素,映射到自己的乘法逆元)换成人话就是(对多项式的逆,参考:链接): 其次,对字节做仿射变换 2 ...
- 感知机:Perceptron Learning Algorithm
感知机是支持向量机SVM和神经网络的基础 f = sign(wx+b) 这样看起来好像是LR是差不多的,LR是用的sigmoid函数,PLA是用的sign符号函数,两者都是线性分类器,主要的差别在于策 ...
- [leetcode]103. Binary Tree Zigzag Level Order Traversal二叉树Z字形层序遍历
相对于102题,稍微改变下方法就行 迭代方法: 在102题的基础上,加上一个变量来判断是不是需要反转 反转的话,当前list在for循环结束后用collection的反转方法就可以实现反转 递归方法: ...
- 华为存储18500 V5配置SNMP
配置流程 配置SNMPV1/V2C 配置端口 admin:/>show snmp port SNMP Listening Port : 161 admin:/>change snmp po ...
- hive集群模式安装
hadoop3.2.0 完全分布式安装 hive-3.1.1 #解压缩tar -zxvf /usr/local/soft/apache-hive-3.1.1-bin.tar.gz -C /usr/lo ...