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. 记一次真实的webpack优化经历

    前言 公司目前现有的一款产品是使用vue v2.0框架实现的,配套的打包工具为webpack v3.0.整个项目大概有80多个vue文件,也算不上什么大型项目. 只不过每次头疼的就是打包所耗费的时间平 ...

  2. 【磁盘/文件系统】第五篇:CentOS7.x__btrfs文件系统详解

    前言: Btrfs文件系统是CentOS7.x系列系统上的技术预览版,但是现在还是有公司在使用. btrfs 文件系统(又称B-tree.Butter FS.Better FS等文件系统)   理解b ...

  3. bootstrap(一)栅格系统

    中文网:http://www.bootcss.com/ 官网:http://v3.bootcss.com/ 需要准备:离线手册  和  软件包 项目中引用bootstrap.min.js压缩版和boo ...

  4. 在GitHub里面如何删除库

    GitHub是一个面向开源及私有软件项目的托管平台,今天将为大家介绍如何在GitHub中彻底删除一个代码仓库. 在GitHub里面如何删除库 1.先进入个人仓库里面 2.点击进入你想要的删除的库 3. ...

  5. SpringBoot入门到精通系列教程 - Filter/WebFilter

    1. Filter简介 1.1. SpringBoot 中Filter的作用 1.2. @WebFilter 2. 基于@WebFilter的实现 2.1. SpringbootApplication ...

  6. 【NC基础操作】开发环境配置初体验

    当我们拿到开发工具UAP-STUDIO-6.5.0.2和Home文件的时候,意味着我们可以用这两样东西开始进行项目开发了(默认其他准备已就绪). 运行UAP-STUDIO-6.5.0.2 双击进入&q ...

  7. 简单4步,利用Prometheus Operator实现自定义指标监控

    本文来自Rancher Labs 在过去的文章中,我们花了相当大的篇幅来聊关于监控的话题.这是因为当你正在管理Kubernetes集群时,一切都会以极快的速度发生变化.因此有一个工具来监控集群的健康状 ...

  8. Spring boot JPA读取数据库方法

    方法1: 1 StringBuffer sb = new StringBuffer(300); 2 sb.append("SELECT v.id, v.container_number, v ...

  9. Pytest测试框架(五):pytest + allure生成测试报告

    Allure 是一款轻量级.支持多语言的开源自动化测试报告生成框架,由Java语言开发,可以集成到 Jenkins. pytest 测试框架支持Allure 报告生成. pytest也可以生成juni ...

  10. .NET 云原生架构师训练营(模块二 基础巩固 RabbitMQ 工作队列和交换机)--学习笔记

    2.6.4 RabbitMQ -- 工作队列和交换机 WorkQueue Publish/Subscribe Routing EmitLog WorkQueue WorkQueue:https://w ...