C++中map、hash_map、unordered_map、unordered_set通俗辨析
2014-08-18 13:17
405 查看
一、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>便运行成功。
三、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/
这两个的内部结构都是采用哈希表来实现。区别在哪里?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>便运行成功。
#include <iostream> #include <set> using namespace std; int main() { set<int> s1; set<int, greater<int> > s2; for (int i = 1; i < 6; ++i) { s1.insert(i); s2.insert(i); } set<int>::const_iterator it = s1.begin(); for(;it != s1.end();it++) cout << *it << endl;//:1 2 3 4 5 it = s2.begin(); for(;it != s2.end();it++) cout << *it << endl;//:5 4 3 2 1 return 0; }
三、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/
class Solution { public: int longestConsecutive(vector<int> &num) { unordered_set<int> set; unordered_set<int>::const_iterator it; for(int i = 0;i<num.size();i++) { if(set.find(num[i]) == set.end()) { set.insert(num[i]); } } int maxLen = 0; for(int i=0;i<num.size();i++) { int tmp = 1; if(set.find(num[i]) != set.end()) { int cur = num[i] + 1; while(set.find(cur) != set.end()) { set.erase(cur); tmp++; cur++; } cur = num[i]-1; while(set.find(cur) != set.end()) { set.erase(cur); tmp++; cur--; } if(tmp>maxLen) maxLen = tmp; } } return maxLen; } };
相关文章推荐
- C++中map、hash_map、unordered_map、unordered_set通俗辨析
- C++中map、hash_map、unordered_map、unordered_set通俗辨析
- C++中map、hash_map、unordered_map、unordered_set通俗辨析
- C++中map、set、hash_map、hash_set、unordered_map、unordered_set通俗辨析
- C++中map、hash_map、unordered_map、unordered_set通俗辨析
- C++中map,hash_map,unordered_map,unordered_set区别与联系
- map 学习(下)——C++ 中的 hash_map, unordered_map
- C++ 头文件系列(unordered_map、unordered_set)
- c++ 标准库的各种容器(vector,deque,map,set,unordered_map,unordered_set,list)的性能考虑
- C++ 头文件系列(unordered_map、unordered_set)
- C++关联容器(unordered_map,vector,unordered_set)
- C++ 头文件系列(unordered_map、unordered_set)
- c++ 标准库的各种容器(vector,deque,map,set,unordered_map,unordered_set,list)的性能考虑
- C++ Primer学习笔记(11)——(转)c++ 标准库的各种容器(vector,deque,map,set,unordered_map,unordered_set,list)的性能考虑
- c++ unordered_map/set自定义对象的hash
- C++中map、hash_map、unordered_map、unordered_set通俗辨析
- C++中map、hash_map、unordered_map、unordered_set通俗辨析
- 【C++】【总结】unordered_map,unordered_set,map和set的用法和区别
- C++ 头文件系列(unordered_map、unordered_set)
- c++ 标准库的各种容器(vector,deque,map,set,unordered_map,unordered_set,list)的性能考虑