C++(标准库):20---STL容器之(无序容器unordered_set、unordered_multiset、unordered_map、unordered_multimap)
2020-04-19 21:19
597 查看
一、无序容器概述
- STL内部预先定义好的无序容器有: unordered_set:是无序元素的集合,其中每个元素不能重复 基础语法参阅:https://blog.csdn.net/qq_41453285/article/details/105483561
- 无序容器通常以hash table实现出来,内部结构是一个“由linked list组成”的array,源码剖析参阅:https://blog.csdn.net/qq_41453285/article/details/104260053
二、都支持的操作
- 本文先介绍这4个无序容器的相同操作,然后介绍各自的操作
- 因为有序的关联容器有的操作,例如insert、find等,无序容器都可以使用。因此通常可以用一个无序容器替换对应的有序容器来完成任务,反之也可以
无序容器的无序性
- 有序的关联容器key值会按序排列,但是无序容器不会,见下面案例
[code]//当使用顺序容器时,会为key自动排序 map<string, int> word_count; //空的 string word; while (cin >> word) { ++word_count[word]; } for (const auto &w : word_count) { cout <<w.first <<" occurs "<<w.second<< ((w.second>1)?" times":" time")<< endl; }
[code]//无序容器的key随机存储 unordered_map<string, int> word_count; //空的 string word; while (cin >> word) { ++word_count[word]; } for (const auto &w : word_count) { cout <<w.first <<" occurs "<<w.second<< ((w.second>1)?" times":" time")<< endl; }
无序容器的管理桶
- 无序容器提供了一组管理桶的函数,这些成员函数允许我们查询容器的状态以及必要时强制容器进行重组。见下面
无序容器对关键字类型的要求
三、unordered_map、unordered_multimap
- 头文件:#include <unordered_map>
unordered_map提供下标操作、unordered_multimap不提供下标操作
- unordered_map的key唯一,提供下标操作;但unordered_multimapkey不唯一,补提供下标操作
- 使用方法:与map类似,见map文章
添加元素(insert、emplace)
- 见map笔记处
删除元素(erase)
- 见map函数,原理相同
其他操作
- 详细介绍,见map容器处
四、unordered_set、unordered_multiset
- 头文件:#include <unordered_set>
都不提供下标操作(下标运算符和at函数)
- 因为只有一个key,没有key与value的对应关系
添加元素(insert、emplace)
- 见map笔记处
删除元素(erase)
- 见map函数,原理相同
其他操作
- 详细介绍,见map容器处
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- C++的STL容器之关联性容器set、multiset、map、multimap
- STL set multiset map multimap unordered_set unordered_map example
- STL set multiset map multimap unordered_set unordered_map example
- c++ 标准库的各种容器(vector,deque,map,set,unordered_map,unordered_set,list)的性能考虑
- STL容器分析--unordered_map/unordered_set(C++11)
- C++标准模板库(STL):set/multiset和map/multimap
- C++:关联容器(set,multiset,map,multimap)
- STL容器 set、 multiset、map、multimap
- c++ 标准库的各种容器(vector,deque,map,set,unordered_map,unordered_set,list)的性能考虑
- 【c++】STL 容器 unordered_map用法详解
- 【C++】【总结】unordered_map,unordered_set,map和set的用法和区别
- map/multimap/unordered_map/unordered_multimap; set/multiset/unordered_set/unordered_multiset
- STL之关联容器(set /map /multiset /multimap)
- STL无序容器之unordered_map
- C++学习笔记(十五)(set/ multiset 容器,map/ multimap容器)
- 关联容器(map、set、multimap、multiset、pair、unordered_map)
- STL关联容器MAP、SET与multimap、multiset与hashmap、hashset的区别
- C++ STL标准库的关联容器set与map
- STL中map,multimap,set,multiset,unordered_map,unordered_multimap,unordered_set,unordered_multiset的实现方法
- 【C++】【总结】unordered_map,unordered_set,map和set的用法和区别