C++ Primer 5th笔记(chap 11)关联容器---无序容器
无序关联容器 unordered associative container
•unordered_map
•unordered_set
•unordered_multimap
•unordered_multiset
1. 定义
- 一个hash函数和==运算符
- 无序容器在存储上组织为一组桶,每个桶保存0到多个元素。
- 对于unordered_map或者unordered_set容器,其遍历顺序与创建该容器时输入元素的顺序是不一定一致的,遍历是按照哈希表从前往后依次遍历的
eg.
unordered_map<string, size_t> word_count;
string word;
while (cin >> word)
{++word_count[word];
}for (auto e : word_count)cout << e.first;for(auto b = word_count.begin();b!= word_count.end();b++) {cout << b->first;
}
2. 无序容器管理操作
操作 | 说明 |
---|---|
桶接口 | |
c.bucket_count() | 正在使用的桶数目 |
c.max_bucket_count() | 容器能容纳的最多的桶的数量 |
c.bucket_size(n) | 第n个桶中有多少个元素 |
c.bucket(k) | 关键字为k的元素在哪个桶中 |
桶迭代 | |
local_iterator | 可以用来访问桶中元素的迭代器类型 |
const_local_iterator | 桶迭代器的const版本 |
c.begin(n), c.end(n) | 桶n的首元素迭代器和尾后迭代器 |
c.cbegin(n), c.cend(n) | 与前两个函数类似,但返回const_local_iterator |
哈希策略 | |
c.load_factor() | 每个桶的平均元素数量,返回float值 |
c.max_load_factor() | c试图维护的平均桶大小,返回float值。c会在需要时添加新的桶,以使得load_factor<=max_load_factor |
c.rehash(n) | 重组存储,使得bucket_count>=n且bucket_count>size/max_load_factor |
c.reserve(n) | 重组存储,使得c可以保存n个元素且不必rehash |
3. 无序容器对关键字类型的要求
提供自己hash函数或==运算符
eg.
class Sales_data {public:int n;Sales_data() {}Sales_data(int) {}bool operator < (const Sales_data& lhs){return true;}bool static compareIsbn(const Sales_data& lhs, const Sales_data& rhs){return true;}std::string isbn() const { return ""; }// how to override default hash and equality operator on key_typesize_t static hasher(const Sales_data& sd){return hash<string>()(sd.isbn());}bool static eqOp(const Sales_data& lhs, const Sales_data& rhs){return lhs.isbn() == rhs.isbn();}// we'll see how to define our own operators in chapter 14bool operator =(const Sales_data& l){return l.n == this->n;}
};typedef unordered_multiset<Sales_data, decltype(Sales_data::hasher)*, decltype(Sales_data::eqOp)*> SD_multiset;SD_multiset bookstore(42, Sales_data::hasher, Sales_data::eqOp);
eg2.
// Foo must have ==
struct Foo { string s; };// we'll see how to define our own operators in chapter 14
bool operator==(const Foo& l, const Foo& r) { return l.s == r.s; }
size_t FooHash(const Foo& f) { return hash<string>()(f.s); }
unordered_set<Foo, decltype(FooHash)*> fooSet(10, FooHash);
【引用】
- 代码
https://github.com/thefistlei/cplusprimer/blob/main/cprimer/cprimer/mapTest.h
C++ Primer 5th笔记(chap 11)关联容器---无序容器相关推荐
- C++ Primer 5th笔记(9)chapter9 顺序容器
1. 定义:元素按位置进行存储和访问.元素的排列次序与元素值无关,而是由元素添加到容器的次序决定的. 容器类型 定义 优点 缺点 string 可变大小数组 支持快速随机访问 在尾部之外的位置插入或删 ...
- C++ Primer 5th笔记(9)chapter9 顺序容器 构造和赋值
1. 容器对象的构造和赋值 1.1 构造 C c; C c; //默认构造函数 C c1(c2) C c1=c2; C c{a,b,c,-} C c={a,b,c,-} C c(b,e)//eg. l ...
- C++ Primer 5th笔记(9)chapter9 顺序容器 string
1. string构造 string s; 定义一个新的空string对象,命名为s string s(cp); 定义一个新的string对象,用cp所指向的(以空字符null结束的)C风格字符串初始 ...
- C++ Primer 5th笔记(9)chapter9 顺序容器 vector 容器的自增长 容器适配器
1. vector 容器的自增长:当插入元素当存储空间不足时,vector 必须重新分配存储空间(比如将新空间大小增加为当前大小的2倍) 管理容量的成员函数 操作 定义 c.shrink_to_fit ...
- C++ Primer 5th笔记(9)chapter9 顺序容器 get insert delete
insert.delete需要解决两个问题: 在什么位置插入? 返回什么位置? 1. 访问对象 操作 定义 c.back(); 返回c中尾元素的引用, 若c为空, 函数行为未定义. c.front() ...
- C++ Primer 5th笔记(9)chapter9 顺序容器 forward_list
1. 特殊的forward_list 单向链表的删除操作,真正删除的是某个元素后面的值(想想是为什么?) 操作 定义 lst.before_begin() 返回指向链表首元素之前不存在的元素的迭代器, ...
- C++ Primer 5th笔记(chap 16 模板和泛型编程)类模板特例化
1. 定义一个特例化版本, 模板参数为 Sales data // 打开std 命名空间, 以便特例化 std::hash namespace std {template struct hash< ...
- C++ Primer 5th笔记(10)chapter10 泛型算法 :迭代器
迭代器类别 名称 定义 插入迭代器 这些迭代器被绑定到一个容器上,可用来向容器插入元素 流迭代器 这些迭代器被绑定到输入或输出流上,可用来遍历所有关联的IO流 反向迭代器 这些迭代器向后而不是向前移动 ...
- C++ Primer 5th笔记(chap 11)关联容器
•map •multimap •set •multiset •set •unordered_map •unordered_set •unordered_multimap •unordered_mult ...
最新文章
- OpenCV+python:读取图片
- python字典键值可以是元组吗_python元组与字典
- oracle 00980,ORA-00980如何解决
- oracle实现主键自动增长
- Spring REST:异常处理卷。 2
- matlab6.1实用指南,MATLAB 6.1实用指南
- 无比乐java游戏_传智播客Java JavaEE+物联网云计算 就业班
- dev控件调用html页面,Chrome开发工具 扩展 DevTools
- TeamViewer可以实现国际连接吗?
- MYSQL have_innodb DISABLED无法创建innodb类型的表
- MS SQL 2008数据库完全卸载指南
- 从零开始学编程——编程语言
- mysql根据15位或者18位身份证号计算年龄的自定义函数
- UWP 如何阻止WebView自动打开浏览器?
- 【论文解读】深度学习目标检测 | R-CNN系列里程碑 | 一文弄懂Faster R-CNN
- 【硬见小百科】开关电源电路组成及各部分详解
- jvm配置垃圾收集参数
- ios 闪退的原因及解决方案
- ssm校园兼职招聘系统计算机毕业设计
- android打包名字,使用jenkins为android工程打包,支持多包名,改资源(踩坑指南)...
热门文章
- Android --- 知识点记录
- Java 洛谷 P1149 火柴棒等式
- 敏捷开发_全面解析瀑布式开发和敏捷式开发
- yolov5论文叫什么_论文格式与论文查重到底是什么鬼?教你半小时搞定的独门技巧...
- 光栅衍射主极大个数_光栅的分类及常规指标介绍
- MAT之PSO:利用PSO实现对一元函数y = sin(10*pi*x) ./ x进行求解优化,找到最优个体适应度
- 成功解决tensorboard调用events.out.tfevents文件得到网址出现No dashboards are active for the current data set问题
- 安装nvidia-docker2
- MongoDB数据库--扩展Base64,算法
- 单片机成长之路(51基础篇) - 008 C51 的标示符和关键字