C++中map、hash_map、unordered_map、unordered_set通俗辨析

来源:互联网 发布:win10优化版 编辑:程序博客网 时间:2024/05/29 04:22

一、hash_map、unordered_map

这两个的内部结构都是采用哈希表来实现。区别在哪里?unordered_map在C++11的时候被引入标准库了,而hash_map没有,所以建议还是使用unordered_map比较好。

哈希表的好处是什么?查询平均时间是O(1)。顾名思义,unordered,就是无序了,数据是按散列函数插入到槽里面去的,数据之间无顺序可言,但是有些时候我只要访问而不需要顺序,因此可以选择哈希表。

二、unordered_map与map

虽然都是map,但是内部结构大大的不同哎,map的内部结构是R-B-tree来实现的,所以保证了一个稳定的动态操作时间,查询、插入、删除都是O(logN),最坏和平均都是。而unordered_map如前所述,是哈希表。顺便提一下,哈希表的查询时间虽然是O(1),但是并不是unordered_map查询时间一定比map短,因为实际情况中还要考虑到数据量,而且unordered_map的hash函数的构造速度也没那么快,所以不能一概而论,应该具体情况具体分析。

multiset  set:

缺省准则less是一个仿函数程序运行会报错。但是如果把s1的排序准则也指定为greater<int>便运行成功。

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <set>  
  3. using namespace std;  
  4.   
  5.   
  6. int main() {  
  7. set<int> s1;  
  8. set<int, greater<int> > s2;  
  9. for (int i = 1; i < 6; ++i) {  
  10. s1.insert(i);  
  11. s2.insert(i);  
  12. }  
  13. set<int>::const_iterator it = s1.begin();  
  14. for(;it != s1.end();it++)  
  15. cout << *it << endl;//:1 2 3 4 5  
  16. it = s2.begin();  
  17. for(;it != s2.end();it++)  
  18. cout << *it << endl;//:5 4 3 2 1  
  19. return 0;  
  20. }  


三、unordered_map与unordered_set

后者就是在哈希表插入value,而这个value就是它自己的key,而不是像之前的unordered_map那样有键-值对,这里单纯就是为了方便查询这些值。我在Longest Consecutive Sequence这一题,我的方法一就是用了unordered_set,同样是一个将空间弥补时间的方法。再举个大家好懂的例子,给你A,B两组数,由整数组成,然后把B中在A中出现的数字取出来,要求用线性时间完成。很明显,这道题应该将A的数放到一个表格,然后线性扫描B,发现存在的就取出。可是A的范围太大,你不可能做一个包含所有整数的表,因为这个域太大了,所以我们就用unordered_set来存放A的数,具体实现库函数已经帮你搞定了,如果对于具体怎么去散列的同学又兴趣可以查看《算法导论》的第11章或者《数据结构与算法分析》的第五章,如果要看《算法导论》的同学我给个建议,完全散列你第一遍的时候可以暂时跳过不看,确实有点复杂。

#include <boost/unordered_set.hpp>
#include <boost/unordered_map.hpp>
using namespace boost;
散列集合简介:

 unordered库提供两个散列集合类unordered_set和unordered_multiset,STLport也提供hash_set和hash_multiset,它们的接口,用法与stl里的标准关联容器set/multiset相同,只是内部使用散列表代替了二叉树实现,因此查找复杂度由数降为常数。

用法举例:http://www.cplusplus.com/reference/unordered_set/unordered_set/

[cpp] view plain copy
  1. class Solution {  
  2. public:  
  3.     int longestConsecutive(vector<int> &num) {  
  4.         unordered_set<int> set;  
  5.         unordered_set<int>::const_iterator it;  
  6.         for(int i = 0;i<num.size();i++) {  
  7.             if(set.find(num[i]) == set.end()) {  
  8.                 set.insert(num[i]);  
  9.             }  
  10.         }  
  11.         int maxLen = 0;  
  12.         for(int i=0;i<num.size();i++) {  
  13.             int tmp = 1;  
  14.             if(set.find(num[i]) != set.end()) {  
  15.                 int cur = num[i] + 1;  
  16.                 while(set.find(cur) != set.end()) {  
  17.                     set.erase(cur);  
  18.                     tmp++;  
  19.                     cur++;  
  20.                       
  21.                 }  
  22.                 cur = num[i]-1;  
  23.                 while(set.find(cur) != set.end()) {  
  24.                      set.erase(cur);  
  25.                     tmp++;  
  26.                     cur--;  
  27.                      
  28.                 }  
  29.                 if(tmp>maxLen)  
  30.                 maxLen = tmp;  
  31.             }  
  32.         }  
  33.         return maxLen;  
  34.     }  
  35. };  
0 0