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): " ...
随机推荐
- SpringBoot从入门到精通教程(二)
SpringBoot 是为了简化 Spring 应用的创建.运行.调试.部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖 ...
- swift学习之label,button,imageView,字符串,数组,字典
import UIKit class ViewController: UIViewController,UITextFieldDelegate { var textField: UITextField ...
- SQLServer之 Stuff和For xml path
示例 昨天遇到一个SQL Server的问题:需要写一个储存过程来处理几个表中的数据,最后问题出在我想将一个表的一个列的多行内容拼接成一行,比如表中有两列数据 : 类别 名称 AAA 企业1 AAA ...
- HTML 防盗链 用src引用网上图片显示 403 Forbidden
比如 <img class="toto" src="http://img5.imgtn.bdimg.com/it/u=152658425,3125530872&am ...
- 使用aspnet_compiler对web程序进行预编译
前言 本例使用的是asp.net中的webform项目,使用.net框架为.net3.5 操作步骤 正常的web项目发布步骤 发布方法:文件系统 目标位置:发布后的项目文件的路径,可自定义. 打开wi ...
- 更改Cmder的λ符号为自定义符号/文字
此次修改cmder Version为1.3.12.915 Step1. 进入\cmder\vendor\目录,打开clink.lua文件,在51行将ocal lambda = "λ" ...
- ConstraintLayout 学习笔记
如何阅读 xml 属性 与 Relativelayout 不同,ConstrainLayout 的属性需要同时说明需要怎么操作自己与目标控件,例如:layout_constraintLeft_toLe ...
- Thymeleaf Shiro标签
记录一下 guest标签 <shiro:guest> </shiro:guest> 用户没有身份验证时显示相应信息,即游客访问信息. user标签 <shiro:user ...
- RabbitMQ不讲武德,发个消息也这么多花招
前言 本篇博客已被收录GitHub:https://zhouwenxing.github.io/ 文中所涉及的源码也已被收录GitHub:https://github.com/zhouwenxing/ ...
- 【并发编程】- ThreadPoolExecutor篇
Executor框架 Executor框架的两级调度模型(基于HotSpot) 在上层,Java多线程程序通常把应用分解为若干个任务,然后使用用户级的调度器(Executor框架)将这些任务映射为固定 ...