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 自定义键类型的更多相关文章

  1. C++ | unordered_map 自定义键类型

    C++ unordered_map 使用自定义类作为键类型 C++ unordered_map using a custom class type as the key

  2. STL: unordered_map 自定义键值使用

    使用Windows下 RECT 类型做unordered_map 键值 1. Hash 函数 计算自定义类型的hash值. struct hash_RECT { size_t operator()(c ...

  3. map自定义键值类型

    map自定义键值类型 改变Map的默认比较方式 https://www.cnblogs.com/zjfdlut/archive/2011/08/12/2135698.html 大家知道,STL中的ma ...

  4. c/c++ 标准库 set 自定义关键字类型与比较函数

    标准库 set 自定义关键字类型与比较函数 问题:哪些类型可以作为标准库set的关键字类型呢??? 答案: 1,任意类型,但是需要额外提供能够比较这种类型的比较函数. 2,这种类型实现了 < 操 ...

  5. c++ 标准库的各种容器(vector,deque,map,set,unordered_map,unordered_set,list)的性能考虑

    转自:http://blog.csdn.net/truexf/article/details/17303263 一.vector vector采用一段连续的内存来存储其元素,向vector添加元素的时 ...

  6. Java用自定义的类型作为HashMap的key

      需要重写hashCode()和equals()方法才可以实现自定义键在HashMap中的查找. public class PhoneNumber { private int prefix; //区 ...

  7. [C#] 类型学习笔记三:自定义值类型

    既前两篇之后,这一篇我们讨论通过struct 关键字自定义值类型. 在第一篇已经讨论过值类型的优势,节省空间,不会触发Gargage Collection等等. 在对性能要求比较高的场景下,通过str ...

  8. wordpress添加post_type自定义文章类型

    wordpress很强大,能当博客也能进行二次开发出很完善的内容管理系统满足企业运营需求,比如可以添加products产品模型.汽车模型等,如何实现呢?添加post_type自定义文章类型就可以了 p ...

  9. Python - Django - ORM 自定义 char 类型字段

    用 CharField 定义的字段在数据库中存放为 verchar 类型 自定义 char 类型字段需要下面的代码: class FixedCharField(models.Field): " ...

随机推荐

  1. 出现VMware Workstation 无法连接到虚拟机。请确保您有权运行该程序、访问该程序使用的所有目录以及访问所有临时文件目录。 未能将管道连接到虚拟机: 所有的管道范例都在使用中。

    今天在学习Linux 的时候 启动VM时出现了这个问题, 搞了很久终于弄好了, 就写篇博客来记录一下,帮助一下大家,如果对大家有帮助,还请大哥大姐点个关注,你的支持就是我坚持下去的动力 ! VMwar ...

  2. 闲鱼上哪些商品抢手?Python 分析后告诉你

    1目 标 场 景 经常看到有朋友在闲鱼卖些小东西又或是自己擅长的一些技能,都能为他们带来不错的 睡后收入. 闲鱼上大量的商品,很难精准判断哪些受欢迎,哪些好卖:一个个录入数据去做数据分析,浪费时间的同 ...

  3. python3全局函数解析

    python的全局函数: import builtins dir(builtins) [ 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray ...

  4. 在飞儿云主机里使用酷Q时遇到相关问题的解决办法

    情况1:酷Q Air版本可以使用,而Pro版本无法运行 解决方法如下: p.p1 { margin: 0; font: 13px "Helvetica Neue"; color: ...

  5. matplotlib学习日记(七)---误差棒图

    (一)误差棒图----误差置信区间的表示 import matplotlib.pyplot as plt import numpy as np x = np.linspace(0.1, 0.6, 10 ...

  6. alibaba-sentinel-1.8变化

    maven最新坐标 <dependencies> <dependency> <groupId>org.springframework.boot</groupI ...

  7. 安装Yii2框架

    一.Windows安装Yii2 1.安装Composer Composer 需要 PHP 5.3.2+ 以上版本,且需要开启 openssl,打开 php 目录下的 php.ini,将 extensi ...

  8. 配置文件中配置集合类(Map、list)@Value注入map、List

    spel表达式就是spring表达式.在java代码中,还有这种写法: @Value("#{'${auth.filter.exclude-urls}'.split(',')}") ...

  9. MongoDb学习(五)---gridfs --http文件下载

    现在网上的文章都是用的低版本的jar包,而最新的jar包,下载的方法进行了改变.在网上也没找到好的方法.就用原生的方法进行下载, 我也不知道对不对.反正可以下载了.就先这样吧.后期准备还是用低版本的开 ...

  10. JavaDailyReports10_10

    1.4.2 键盘事件的处理 KeyListener  接口实现了处理键盘事件      KeyEvent 对象描述键盘事件的相关信息. KeyListener 接口有三个方法:KeyPressed K ...