【C++】【总结】unordered_map,unordered_set,map和set的用法和区别

参考链接: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等容器,另外使用中序遍历可将键值按照从小到大遍历出来。




通用数据:

[cpp]  view plain  copy
  1. vector<int> list;  
  2.     list.push_back(5);  
  3.     list.push_back(14);  
  4.     list.push_back(34);  
  5.     list.push_back(22);  
  6.     list.push_back(39);  
  7.     list.push_back(5);  

unordered_map

头文件:#include<unordered_map>

介绍:std::unordered_map 就是以key来查找value而设计,不会根据key排序。

代码:

[cpp]  view plain  copy
  1. unordered_map<intint> map;  
  2.        for (int i=0; i<list.size(); i++){  
  3.            map[i] = list[i];  
  4.        }  
  5.        cout << map[0] << endl;  
  6.        for (unordered_map<intint>::iterator i = map.begin(); i != map.end(); i++){  
  7.            cout << i->first << ' ' << i->second << endl;  
  8.        }  
  9.        if (map.find(3) != map.end()) {  
  10.            cout << "find key=" << map.find(3)->first << ", value=" << map.find(3)->second << endl;  
  11.        }  
  12.        if (map.count(5) > 0) {  
  13.            cout << "find 5: " << map.count(5) << endl;  
  14.        }  
结果:

UnorderedMap

[cpp]  view plain  copy
  1. 5  
  2. 5 5  
  3. 4 39  
  4. 3 22  
  5. 2 34  
  6. 1 14  
  7. 0 5  
  8. find key=3, value=22  
  9. find 5: 1  


==============================================


m.count(n)计算下标为n的位置有无数据,有返回1,无返回0。

==============================================


unordered_map也有find方法,得到的对象是一个iterator




unordered_set

头文件:#include<unordered_set>

介绍:std::unordered_set 是基于hash表的,因此并不是顺序存储。

代码:

[cpp]  view plain  copy
  1. unordered_set<int> set;  
  2.        for (int i=0; i<list.size(); i++){  
  3.            set.insert(list[i]);  
  4.        }  
  5.        for (unordered_set<int>::iterator i = set.begin(); i != set.end(); i++) {  
  6.            cout << *i << endl;  
  7.        }  
  8.        cout << " find 39: " << *set.find(39) << endl;  
  9.        cout << "count 14:" << set.count(5) << endl;  
结果:

[cpp]  view plain  copy
  1. UnorderdSet  
  2. 22  
  3. 39  
  4. 34  
  5. 14  
  6. 5  
  7.  find 39: 39  
  8. count 14:1  

map

头文件:#include<map>

介绍:std::map 就是以key来查找value而设计,根据key排序。

代码:

[cpp]  view plain  copy
  1. map<intint> map1;  
  2.         for (int i=0; i<list.size(); i++){  
  3.             map1[i] = list[i];  
  4.         }  
  5.         for (map<intint>::iterator i = map1.begin(); i != map1.end(); i++){  
  6.             cout << i->first << ' ' << i->second << endl;  
  7.         }  
  8.         if (map1.find(3) != map1.end()) {  
  9.             cout << "find key=" << map1.find(3)->first << ", value=" << map1.find(3)->second << endl;  
  10.         }  
  11.         if (map1.count(5) > 0) {  
  12.             cout << "count 5: " << map1.count(5) << endl;  
  13.         }  
结果:

[cpp]  view plain  copy
  1. Map  
  2. 0 5  
  3. 1 14  
  4. 2 34  
  5. 3 22  
  6. 4 39  
  7. 5 5  
  8. find key=3, value=22  
  9. count 5: 1  

set

头文件:#include<set>

介绍:std::set 是基于hash表的,因此并不是顺序存储。

代码:

[cpp]  view plain  copy
  1. set<int> set;  
  2.         for (int i=0; i<list.size(); i++){  
  3.             set.insert(list[i]);  
  4.         }  
  5.         for (auto i = set.begin(); i != set.end(); i++) {  
  6.             cout << *i << endl;  
  7.         }  
  8.         cout << *set.find(5) << endl;  
  9.         cout << set.count(5) << endl;  
结果:

[cpp]  view plain  copy
  1. Set  
  2. 5  
  3. 14  
  4. 22  
  5. 34  
  6. 39  
  7. 5  
  8. 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容器,其遍历顺序与创建该容器时输入元素的顺序是不一定一致的,遍历是按照哈希表从前往后依次遍历的

猜你喜欢

转载自blog.csdn.net/mdjxy63/article/details/80056700