参考链接:https://blog.csdn.net/zjajgyy/article/details/65935473
通过代码来区别
unordered_map和map
unordered_map存储机制是哈希表,,即unordered_map内部元素是无序的。
map是红黑树,map中的元素是按照二叉搜索树存储,进行中序遍历会得到有序遍历。
unordered_set和set
unordered_set基于哈希表,是无序的。set实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,以保证每个子树根节点键值大于左子树所有节点的键值,小于右子树所有节点的键值;另外,还得保证根节点左子树的高度与右子树高度相等。
平衡二叉检索树使用中序遍历算法,检索效率高于vector、deque和list等容器,另外使用中序遍历可将键值按照从小到大遍历出来。
通用数据:
- vector<int> list;
- list.push_back(5);
- list.push_back(14);
- list.push_back(34);
- list.push_back(22);
- list.push_back(39);
- list.push_back(5);
unordered_map
头文件:#include<unordered_map>
介绍:std::unordered_map 就是以key来查找value而设计,不会根据key排序。
代码:
- unordered_map<int, int> map;
- for (int i=0; i<list.size(); i++){
- map[i] = list[i];
- }
- cout << map[0] << endl;
- for (unordered_map<int, int>::iterator i = map.begin(); i != map.end(); i++){
- cout << i->first << ' ' << i->second << endl;
- }
- if (map.find(3) != map.end()) {
- cout << "find key=" << map.find(3)->first << ", value=" << map.find(3)->second << endl;
- }
- if (map.count(5) > 0) {
- cout << "find 5: " << map.count(5) << endl;
- }
UnorderedMap
- 5
- 5 5
- 4 39
- 3 22
- 2 34
- 1 14
- 0 5
- find key=3, value=22
- find 5: 1
==============================================
m.count(n)计算下标为n的位置有无数据,有返回1,无返回0。
==============================================
unordered_map也有find方法,得到的对象是一个iterator
unordered_set
头文件:#include<unordered_set>
介绍:std::unordered_set 是基于hash表的,因此并不是顺序存储。
代码:
- unordered_set<int> set;
- for (int i=0; i<list.size(); i++){
- set.insert(list[i]);
- }
- for (unordered_set<int>::iterator i = set.begin(); i != set.end(); i++) {
- cout << *i << endl;
- }
- cout << " find 39: " << *set.find(39) << endl;
- cout << "count 14:" << set.count(5) << endl;
- UnorderdSet
- 22
- 39
- 34
- 14
- 5
- find 39: 39
- count 14:1
map
头文件:#include<map>
介绍:std::map 就是以key来查找value而设计,根据key排序。
代码:
- map<int, int> map1;
- for (int i=0; i<list.size(); i++){
- map1[i] = list[i];
- }
- for (map<int, int>::iterator i = map1.begin(); i != map1.end(); i++){
- cout << i->first << ' ' << i->second << endl;
- }
- if (map1.find(3) != map1.end()) {
- cout << "find key=" << map1.find(3)->first << ", value=" << map1.find(3)->second << endl;
- }
- if (map1.count(5) > 0) {
- cout << "count 5: " << map1.count(5) << endl;
- }
- Map
- 0 5
- 1 14
- 2 34
- 3 22
- 4 39
- 5 5
- find key=3, value=22
- count 5: 1
set
头文件:#include<set>
介绍:std::set 是基于hash表的,因此并不是顺序存储。
代码:
- set<int> set;
- for (int i=0; i<list.size(); i++){
- set.insert(list[i]);
- }
- for (auto i = set.begin(); i != set.end(); i++) {
- cout << *i << endl;
- }
- cout << *set.find(5) << endl;
- cout << set.count(5) << endl;
- Set
- 5
- 14
- 22
- 34
- 39
- 5
- 1
==================================================
c++中map与unordered_map的区别
头文件
- map: #include < map >
- unordered_map: #include < unordered_map >
内部实现机理
- map: map内部实现了一个红黑树,该结构具有自动排序的功能,因此map内部的所有元素都是有序的,红黑树的每一个节点都代表着map的一个元素,因此,对于map进行的查找,删除,添加等一系列的操作都相当于是对红黑树进行这样的操作,故红黑树的效率决定了map的效率。
- unordered_map: unordered_map内部实现了一个哈希表,因此其元素的排列顺序是杂乱的,无序的
优缺点以及适用处
- map
- 优点:
- 有序性,这是map结构最大的优点,其元素的有序性在很多应用中都会简化很多的操作
- 红黑树,内部实现一个红黑书使得map的很多操作在[Math Processing Error]lgn的时间复杂度下就可以实现,因此效率非常的高
- 缺点:
- 空间占用率高,因为map内部实现了红黑树,虽然提高了运行效率,但是因为每一个节点都需要额外保存父节点,孩子节点以及红/黑性质,使得每一个节点都占用大量的空间
- 适用处,对于那些有顺序要求的问题,用map会更高效一些
- 优点:
- unordered_map
- 优点:
- 因为内部实现了哈希表,因此其查找速度非常的快
- 缺点:
- 哈希表的建立比较耗费时间
- 适用处,对于查找问题,unordered_map会更加高效一些,因此遇到查找问题,常会考虑一下用unordered_map
- 优点:
note:
- 对于unordered_map或者unordered_set容器,其遍历顺序与创建该容器时输入元素的顺序是不一定一致的,遍历是按照哈希表从前往后依次遍历的