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): " ...
随机推荐
- VSCode + PYQT5 搭建图形化界面
1,安装依赖 pip install -i https://mirrors.aliyun.com/pypi/simple/ PyQt5 pip install -i https://mirrors.a ...
- 6 个例子教你重构 Python 代码
1. 合并嵌套的 if 条件 太多的嵌套会使代码难以理解,这在 Python 中尤为如此,因为 Python 没有括号来帮助区隔不同的嵌套级别. 阅读深度嵌套的代码容易让人烦躁,因为你必须理清哪些条件 ...
- 顶会两篇论文连发,华为云医疗AI低调中崭露头角
摘要:2020年国际医学图像计算和计算机辅助干预会议(MICCAI 2020),论文接收结果已经公布.华为云医疗AI团队和华中科技大学合作的2篇研究成果入选. 同时两篇研究成果被行业顶会收录,华为云医 ...
- Visual Studio 默认git拉取Github出错 No error could not read Username for 'https://github.com': terminal prompts disabled
发布到远程存储库时遇到错误: Git failed with a fatal error.fatal: HttpRequestException encountered. ��������ʱ��� ...
- Java“微服务”还能这么玩!
"微服务"加个引号是因为这不是传统定义的微服务架构,顶多算是"小服务"架构,因为服务实例由集群节点统一加载,非独立部署.下面以图说明一下服务调用流程. 一. ...
- Android 安全研究 hook 神器frida学习(一)
在进行安卓安全研究时,hook技术是不可或缺的,常用的有Xposed:Java层的HOOK框架,由于要修改Zgote进程,需要Root,体验过Xposed,整个过程还是很繁琐的,并且无法hook,na ...
- Java 8 新特性 - Lambda表达式
Lambda表达式 vs 匿名类既然lambda表达式即将正式取代Java代码中的匿名内部类,那么有必要对二者做一个比较分析.一个关键的不同点就是关键字 this.匿名类的 this 关键字指向匿名类 ...
- Plugin 插件体系
Solon 的插件也可以叫扩展组件,相当于Spring 的 starter.Solon已经提供了大量的基础插件,但对第三方的框架适配目前较少. 插件 说明 boot插件:: 说明 org.noear: ...
- 美团关于分布式ID实践方案
在复杂分布式系统中,往往需要对大量的数据和消息进行唯一标识.如在美团点评的金融.支付.餐饮.酒店.猫眼电影等产品的系统中,数据日渐增长,对数据分库分表后需要有一个唯一ID来标识一条数据或消息,数据库的 ...
- 自动化运维工具-Ansible之1-基础
自动化运维工具-Ansible之1-基础 目录 自动化运维工具-Ansible之1-基础 Ansible 基本概述 定义 特点 架构 工作原理 任务执行模式 命令执行过程 Ansible 安装 Ans ...