源代码来自sgi-2.91版本stl_hash_set.h
文章目录
unordered_set总述
先了解hash表
(1)底层实现完全由hash表代替,最开始叫做hash_set,从实现原理来说unordered_set应该被称为容器适配器
(2)对底层hash表来说,键值就是实值
(3)hash表没有自动排序,所以相对于set,unordered_set没有自动排序
(4)unordered_set适合用于只查找元素的情况
unordered_set类
#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class Value, class HashFcn = hash<Value>, //hash函数来对元素编号
class EqualKey = equal_to<Value>, //比较==函数
class Alloc = alloc> //allco分配器
#else
template <class Value, class HashFcn, class EqualKey, class Alloc = alloc> //自定义参数
#endif
class hash_set
{
private:
typedef hashtable<Value, Value, HashFcn, identity<Value>,
EqualKey, Alloc> ht; //申明hash表
ht rep; //创建hash表
public:
typedef typename ht::key_type key_type;
typedef typename ht::value_type value_type;
typedef typename ht::hasher hasher;
typedef typename ht::key_equal key_equal;
typedef typename ht::size_type size_type;
typedef typename ht::difference_type difference_type;
typedef typename ht::const_pointer