总结

  1. 底层是hashtable,因此元素之间是无序的
  2. 性能优于set,但是占用内存会更大
  3. 主要调用insert_unique方法进行插入
  4. 插入值的方法时insert,没有push。push方法对应的容器都是有特性的,比如stack、queue、priority_queue,而push_back/push_front则是对应vector/deque
  5. 正向迭代器,即只支持++p操作,不允许p+=n这种随机访问

hash_set结构

  1. #ifndef __SGI_STL_INTERNAL_HASH_SET_H
  2. #define __SGI_STL_INTERNAL_HASH_SET_H
  3. #include <concept_checks.h>
  4. __STL_BEGIN_NAMESPACE
  5. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  6. #pragma set woff 1174
  7. #pragma set woff 1375
  8. #endif
  9. //hash_set的底层是基于hash table的,hash table没有提供排序,所以hash_set容器里面的内容是没排序的//hash_set和set一样,键值和实值相同,并且键值是唯一的
  10. // Forward declaration of equality operator; needed for friend declaration.
  11. //这里提供默认的参数,其中哈希函数在<stl_hash_fun.h>定义//前面的博文也对哈希函数进行讲解了//http://blog.csdn.net/chenhanzhun/article/details/39584025//用户可自行制定
  12. template <class _Value,
  13. class _HashFcn __STL_DEPENDENT_DEFAULT_TMPL(hash<_Value>),
  14. class _EqualKey __STL_DEPENDENT_DEFAULT_TMPL(equal_to<_Value>),
  15. class _Alloc = __STL_DEFAULT_ALLOCATOR(_Value) >
  16. class hash_set;
  17. template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
  18. inline bool
  19. operator==(const hash_set<_Value,_HashFcn,_EqualKey,_Alloc>& __hs1,
  20. const hash_set<_Value,_HashFcn,_EqualKey,_Alloc>& __hs2);
  21. template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
  22. class hash_set{
  23. // requirements:
  24. __STL_CLASS_REQUIRES(_Value, _Assignable);
  25. __STL_CLASS_UNARY_FUNCTION_CHECK(_HashFcn, size_t, _Value);
  26. __STL_CLASS_BINARY_FUNCTION_CHECK(_EqualKey, bool, _Value, _Value);
  27. private:
  28. //_Identity获取value值,在hash_set中也是键值,_Identity<>定义在<stl_function.h>
  29. /*
  30. template <class _Arg, class _Result>
  31. struct unary_function {
  32. typedef _Arg argument_type;
  33. typedef _Result result_type;
  34. };
  35. template <class _Tp>
  36. struct _Identity : public unary_function<_Tp,_Tp> {
  37. const _Tp& operator()(const _Tp& __x) const { return __x; }
  38. };
  39. */
  40. typedef hashtable<_Value, _Value, _HashFcn, _Identity<_Value>,
  41. _EqualKey, _Alloc> _Ht;
  42. _Ht _M_ht;//hash_set的底层机制是hash table
  43. public:
  44. //以下的内嵌类型均来时hash table
  45. typedef typename _Ht::key_type key_type;
  46. typedef typename _Ht::value_type value_type;
  47. typedef typename _Ht::hasher hasher;
  48. typedef typename _Ht::key_equal key_equal;
  49. typedef typename _Ht::size_type size_type;
  50. typedef typename _Ht::difference_type difference_type;
  51. // 注意: 不能修改hash table内部的元素,reference, pointer, iterator都为const
  52. typedef typename _Ht::const_pointer pointer;
  53. typedef typename _Ht::const_pointer const_pointer;
  54. typedef typename _Ht::const_reference reference;
  55. typedef typename _Ht::const_reference const_reference;
  56. typedef typename _Ht::const_iterator iterator;
  57. typedef typename _Ht::const_iterator const_iterator;
  58. typedef typename _Ht::allocator_type allocator_type;
  59. //返回hash函数
  60. hasher hash_funct() const { return _M_ht.hash_funct(); }
  61. key_equal key_eq() const { return _M_ht.key_eq(); }
  62. allocator_type get_allocator() const { return _M_ht.get_allocator(); }
  63. public:
  64. //构造函数
  65. //缺省情况使用大小为100,但是实际分配的空间大小为不小于100的最小素数
  66. //只是空的hash_set,不存储元素节点
  67. hash_set()
  68. : _M_ht(100, hasher(), key_equal(), allocator_type()) {}
  69. //指定大小n的hash_set表
  70. explicit hash_set(size_type __n)
  71. : _M_ht(__n, hasher(), key_equal(), allocator_type()) {}
  72. //指定大小为n,且指定hash函数的hash_set
  73. hash_set(size_type __n, const hasher& __hf)
  74. : _M_ht(__n, __hf, key_equal(), allocator_type()) {}
  75. //指定大小为n,且指定hash函数和键值比较函数的hash_set
  76. hash_set(size_type __n, const hasher& __hf, const key_equal& __eql,
  77. const allocator_type& __a = allocator_type())
  78. : _M_ht(__n, __hf, __eql, __a) {}
  79. #ifdef __STL_MEMBER_TEMPLATES
  80. //以下hash_set的插入操作使用hash table的insert_unique插入
  81. //不允许有相同的键值插入
  82. //用某个范围的元素初始化hash_set对象
  83. //相当于把某个范围[f,l)插入到空的hash_set
  84. template <class _InputIterator>
  85. hash_set(_InputIterator __f, _InputIterator __l)
  86. : _M_ht(100, hasher(), key_equal(), allocator_type())
  87. { _M_ht.insert_unique(__f, __l); }//调用hash table的插入函数
  88. template <class _InputIterator>
  89. hash_set(_InputIterator __f, _InputIterator __l, size_type __n)
  90. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  91. { _M_ht.insert_unique(__f, __l); }
  92. template <class _InputIterator>
  93. hash_set(_InputIterator __f, _InputIterator __l, size_type __n,
  94. const hasher& __hf)
  95. : _M_ht(__n, __hf, key_equal(), allocator_type())
  96. { _M_ht.insert_unique(__f, __l); }
  97. template <class _InputIterator>
  98. hash_set(_InputIterator __f, _InputIterator __l, size_type __n,
  99. const hasher& __hf, const key_equal& __eql,
  100. const allocator_type& __a = allocator_type())
  101. : _M_ht(__n, __hf, __eql, __a)
  102. { _M_ht.insert_unique(__f, __l); }
  103. #else
  104. hash_set(const value_type* __f, const value_type* __l)
  105. : _M_ht(100, hasher(), key_equal(), allocator_type())
  106. { _M_ht.insert_unique(__f, __l); }
  107. hash_set(const value_type* __f, const value_type* __l, size_type __n)
  108. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  109. { _M_ht.insert_unique(__f, __l); }
  110. hash_set(const value_type* __f, const value_type* __l, size_type __n,
  111. const hasher& __hf)
  112. : _M_ht(__n, __hf, key_equal(), allocator_type())
  113. { _M_ht.insert_unique(__f, __l); }
  114. hash_set(const value_type* __f, const value_type* __l, size_type __n,
  115. const hasher& __hf, const key_equal& __eql,
  116. const allocator_type& __a = allocator_type())
  117. : _M_ht(__n, __hf, __eql, __a)
  118. { _M_ht.insert_unique(__f, __l); }
  119. hash_set(const_iterator __f, const_iterator __l)
  120. : _M_ht(100, hasher(), key_equal(), allocator_type())
  121. { _M_ht.insert_unique(__f, __l); }
  122. hash_set(const_iterator __f, const_iterator __l, size_type __n)
  123. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  124. { _M_ht.insert_unique(__f, __l); }
  125. hash_set(const_iterator __f, const_iterator __l, size_type __n,
  126. const hasher& __hf)
  127. : _M_ht(__n, __hf, key_equal(), allocator_type())
  128. { _M_ht.insert_unique(__f, __l); }
  129. hash_set(const_iterator __f, const_iterator __l, size_type __n,
  130. const hasher& __hf, const key_equal& __eql,
  131. const allocator_type& __a = allocator_type())
  132. : _M_ht(__n, __hf, __eql, __a)
  133. { _M_ht.insert_unique(__f, __l); }
  134. #endif /*__STL_MEMBER_TEMPLATES */
  135. public:
  136. //以下的函数操作只是调用hash table的成员函数
  137. size_type size() const { return _M_ht.size(); }
  138. size_type max_size() const { return _M_ht.max_size(); }
  139. bool empty() const { return _M_ht.empty(); }
  140. void swap(hash_set& __hs) { _M_ht.swap(__hs._M_ht); }
  141. #ifdef __STL_MEMBER_TEMPLATES
  142. template <class _Val, class _HF, class _EqK, class _Al>
  143. friend bool operator== (const hash_set<_Val, _HF, _EqK, _Al>&,
  144. const hash_set<_Val, _HF, _EqK, _Al>&);
  145. #else /* __STL_MEMBER_TEMPLATES */
  146. friend bool __STD_QUALIFIER
  147. operator== __STL_NULL_TMPL_ARGS (const hash_set&, const hash_set&);
  148. #endif /* __STL_MEMBER_TEMPLATES */
  149. iterator begin() const { return _M_ht.begin(); }
  150. iterator end() const { return _M_ht.end(); }
  151. public:
  152. /*
  153. 插入元素
  154. (1)
  155. pair<iterator,bool> insert ( const value_type& val );
  156. (2)
  157. template <class InputIterator>
  158. void insert ( InputIterator first, InputIterator last );
  159. */
  160. //不允许有重复的键值
  161. //返回pair第二个参数second若为true则插入成功
  162. pair<iterator, bool> insert(const value_type& __obj)
  163. {
  164. //调用hash table的insert_unique()函数
  165. pair<typename _Ht::iterator, bool> __p = _M_ht.insert_unique(__obj);
  166. return pair<iterator,bool>(__p.first, __p.second);
  167. }
  168. #ifdef __STL_MEMBER_TEMPLATES
  169. template <class _InputIterator>
  170. void insert(_InputIterator __f, _InputIterator __l)
  171. { _M_ht.insert_unique(__f,__l); }
  172. #else
  173. void insert(const value_type* __f, const value_type* __l) {
  174. _M_ht.insert_unique(__f,__l);
  175. }
  176. void insert(const_iterator __f, const_iterator __l)
  177. {_M_ht.insert_unique(__f, __l); }
  178. #endif /*__STL_MEMBER_TEMPLATES */
  179. pair<iterator, bool> insert_noresize(const value_type& __obj)
  180. {
  181. pair<typename _Ht::iterator, bool> __p =
  182. _M_ht.insert_unique_noresize(__obj);
  183. return pair<iterator, bool>(__p.first, __p.second);
  184. }
  185. //查找键值对应的元素,并且返回该元素的节点位置
  186. iterator find(const key_type& __key) const { return _M_ht.find(__key); }
  187. //返回键值为key的节点元素个数
  188. size_type count(const key_type& __key) const { return _M_ht.count(__key); }
  189. //Returns the bounds of a range that includes all the elements that compare equal to k.
  190. //In hash_set containers, where keys are unique, the range will include one element at most.
  191. pair<iterator, iterator> equal_range(const key_type& __key) const
  192. { return _M_ht.equal_range(__key); }
  193. //擦除指定键值的元素,并返回擦除的个数
  194. //因为键值唯一,则该键值的元素最多为1个
  195. size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
  196. //擦除指定位置的元素
  197. void erase(iterator __it) { _M_ht.erase(__it); }
  198. //擦除指定范围的元素
  199. void erase(iterator __f, iterator __l) { _M_ht.erase(__f, __l); }
  200. //清除hash_set容器
  201. void clear() { _M_ht.clear(); }
  202. public:
  203. //调整hash_set容器的容量
  204. void resize(size_type __hint) { _M_ht.resize(__hint); }
  205. //Returns the number of buckets in the hash_set container.
  206. size_type bucket_count() const { return _M_ht.bucket_count(); }
  207. size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
  208. //Returns the number of elements in bucket n
  209. size_type elems_in_bucket(size_type __n) const
  210. { return _M_ht.elems_in_bucket(__n); }//返回指定桶子键值key中list链表的元素个数};
  211. template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
  212. inline bool
  213. operator==(const hash_set<_Value,_HashFcn,_EqualKey,_Alloc>& __hs1,
  214. const hash_set<_Value,_HashFcn,_EqualKey,_Alloc>& __hs2){
  215. return __hs1._M_ht == __hs2._M_ht;}
  216. #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
  217. template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
  218. inline bool
  219. operator!=(const hash_set<_Value,_HashFcn,_EqualKey,_Alloc>& __hs1,
  220. const hash_set<_Value,_HashFcn,_EqualKey,_Alloc>& __hs2) {
  221. return !(__hs1 == __hs2);}
  222. //交换两个hash_set容器的内容
  223. template <class _Val, class _HashFcn, class _EqualKey, class _Alloc>
  224. inline void
  225. swap(hash_set<_Val,_HashFcn,_EqualKey,_Alloc>& __hs1,
  226. hash_set<_Val,_HashFcn,_EqualKey,_Alloc>& __hs2){
  227. __hs1.swap(__hs2);}
  228. #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */