总结

  1. 底层是hashtable
  2. 操作时使用的是 insert_equal() 方法,因此允许插入重复值
  3. 其余操作与hash_set一致

hash_multiset结构

  1. //以下是hash_multiset的定义/*
  2. hash_multiset是基于底层机制为hash table,hash_multiset的元素不会自动排序,其他功能与multiset类似,
  3. hash_multiset与hash_set的区别和multiset与set区别一样,
  4. hash_multiset的插入函数是hash table的insert_equal()
  5. */
  6. template <class _Value,
  7. class _HashFcn __STL_DEPENDENT_DEFAULT_TMPL(hash<_Value>),
  8. class _EqualKey __STL_DEPENDENT_DEFAULT_TMPL(equal_to<_Value>),
  9. class _Alloc = __STL_DEFAULT_ALLOCATOR(_Value) >
  10. class hash_multiset;
  11. template <class _Val, class _HashFcn, class _EqualKey, class _Alloc>
  12. inline bool
  13. operator==(const hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs1,
  14. const hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs2);
  15. template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
  16. class hash_multiset{
  17. // requirements:
  18. __STL_CLASS_REQUIRES(_Value, _Assignable);
  19. __STL_CLASS_UNARY_FUNCTION_CHECK(_HashFcn, size_t, _Value);
  20. __STL_CLASS_BINARY_FUNCTION_CHECK(_EqualKey, bool, _Value, _Value);
  21. private:
  22. //_Identity获取value值,在hash_set中也是键值,_Identity<>定义在<stl_function.h>
  23. /*
  24. template <class _Arg, class _Result>
  25. struct unary_function {
  26. typedef _Arg argument_type;
  27. typedef _Result result_type;
  28. };
  29. template <class _Tp>
  30. struct _Identity : public unary_function<_Tp,_Tp> {
  31. const _Tp& operator()(const _Tp& __x) const { return __x; }
  32. };
  33. */
  34. typedef hashtable<_Value, _Value, _HashFcn, _Identity<_Value>,
  35. _EqualKey, _Alloc> _Ht;
  36. _Ht _M_ht;//底层机制是hashtable
  37. public:
  38. typedef typename _Ht::key_type key_type;
  39. typedef typename _Ht::value_type value_type;
  40. typedef typename _Ht::hasher hasher;
  41. typedef typename _Ht::key_equal key_equal;
  42. typedef typename _Ht::size_type size_type;
  43. typedef typename _Ht::difference_type difference_type;
  44. // 注意: 不能修改hash table内部的元素,reference, pointer, iterator都为const
  45. typedef typename _Ht::const_pointer pointer;
  46. typedef typename _Ht::const_pointer const_pointer;
  47. typedef typename _Ht::const_reference reference;
  48. typedef typename _Ht::const_reference const_reference;
  49. typedef typename _Ht::const_iterator iterator;
  50. typedef typename _Ht::const_iterator const_iterator;
  51. typedef typename _Ht::allocator_type allocator_type;
  52. //返回hash函数
  53. hasher hash_funct() const { return _M_ht.hash_funct(); }
  54. key_equal key_eq() const { return _M_ht.key_eq(); }
  55. allocator_type get_allocator() const { return _M_ht.get_allocator(); }
  56. public:
  57. //构造函数和hash_set一样,具体可以参照hash_set的讲解
  58. //唯一的区别就是插入函数是insert_equal()
  59. hash_multiset()
  60. : _M_ht(100, hasher(), key_equal(), allocator_type()) {}
  61. explicit hash_multiset(size_type __n)
  62. : _M_ht(__n, hasher(), key_equal(), allocator_type()) {}
  63. hash_multiset(size_type __n, const hasher& __hf)
  64. : _M_ht(__n, __hf, key_equal(), allocator_type()) {}
  65. hash_multiset(size_type __n, const hasher& __hf, const key_equal& __eql,
  66. const allocator_type& __a = allocator_type())
  67. : _M_ht(__n, __hf, __eql, __a) {}
  68. #ifdef __STL_MEMBER_TEMPLATES
  69. template <class _InputIterator>
  70. hash_multiset(_InputIterator __f, _InputIterator __l)
  71. : _M_ht(100, hasher(), key_equal(), allocator_type())
  72. { _M_ht.insert_equal(__f, __l); }
  73. template <class _InputIterator>
  74. hash_multiset(_InputIterator __f, _InputIterator __l, size_type __n)
  75. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  76. { _M_ht.insert_equal(__f, __l); }
  77. template <class _InputIterator>
  78. hash_multiset(_InputIterator __f, _InputIterator __l, size_type __n,
  79. const hasher& __hf)
  80. : _M_ht(__n, __hf, key_equal(), allocator_type())
  81. { _M_ht.insert_equal(__f, __l); }
  82. template <class _InputIterator>
  83. hash_multiset(_InputIterator __f, _InputIterator __l, size_type __n,
  84. const hasher& __hf, const key_equal& __eql,
  85. const allocator_type& __a = allocator_type())
  86. : _M_ht(__n, __hf, __eql, __a)
  87. { _M_ht.insert_equal(__f, __l); }
  88. #else
  89. hash_multiset(const value_type* __f, const value_type* __l)
  90. : _M_ht(100, hasher(), key_equal(), allocator_type())
  91. { _M_ht.insert_equal(__f, __l); }
  92. hash_multiset(const value_type* __f, const value_type* __l, size_type __n)
  93. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  94. { _M_ht.insert_equal(__f, __l); }
  95. hash_multiset(const value_type* __f, const value_type* __l, size_type __n,
  96. const hasher& __hf)
  97. : _M_ht(__n, __hf, key_equal(), allocator_type())
  98. { _M_ht.insert_equal(__f, __l); }
  99. hash_multiset(const value_type* __f, const value_type* __l, size_type __n,
  100. const hasher& __hf, const key_equal& __eql,
  101. const allocator_type& __a = allocator_type())
  102. : _M_ht(__n, __hf, __eql, __a)
  103. { _M_ht.insert_equal(__f, __l); }
  104. hash_multiset(const_iterator __f, const_iterator __l)
  105. : _M_ht(100, hasher(), key_equal(), allocator_type())
  106. { _M_ht.insert_equal(__f, __l); }
  107. hash_multiset(const_iterator __f, const_iterator __l, size_type __n)
  108. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  109. { _M_ht.insert_equal(__f, __l); }
  110. hash_multiset(const_iterator __f, const_iterator __l, size_type __n,
  111. const hasher& __hf)
  112. : _M_ht(__n, __hf, key_equal(), allocator_type())
  113. { _M_ht.insert_equal(__f, __l); }
  114. hash_multiset(const_iterator __f, const_iterator __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_equal(__f, __l); }
  119. #endif /*__STL_MEMBER_TEMPLATES */
  120. public:
  121. //Returns the number of elements in the hash_multiset container
  122. size_type size() const { return _M_ht.size(); }
  123. //Returns the maximum number of elements that the hash_multiset container can hold.
  124. size_type max_size() const { return _M_ht.max_size(); }
  125. //Returns a bool value indicating whether the hash_multiset container is empty,
  126. //i.e. whether its size is 0.
  127. bool empty() const { return _M_ht.empty(); }
  128. //交换两个hash_multiset的内容
  129. void swap(hash_multiset& hs) { _M_ht.swap(hs._M_ht); }
  130. #ifdef __STL_MEMBER_TEMPLATES
  131. template <class _Val, class _HF, class _EqK, class _Al>
  132. friend bool operator== (const hash_multiset<_Val, _HF, _EqK, _Al>&,
  133. const hash_multiset<_Val, _HF, _EqK, _Al>&);
  134. #else /* __STL_MEMBER_TEMPLATES */
  135. friend bool __STD_QUALIFIER
  136. operator== __STL_NULL_TMPL_ARGS (const hash_multiset&,const hash_multiset&);
  137. #endif /* __STL_MEMBER_TEMPLATES */
  138. iterator begin() const { return _M_ht.begin(); }
  139. iterator end() const { return _M_ht.end(); }
  140. public:
  141. //插入元素
  142. /*
  143. iterator insert ( const value_type& val );
  144. template <class InputIterator>
  145. void insert ( InputIterator first, InputIterator last );
  146. */
  147. iterator insert(const value_type& __obj)
  148. { return _M_ht.insert_equal(__obj); }
  149. #ifdef __STL_MEMBER_TEMPLATES
  150. template <class _InputIterator>
  151. void insert(_InputIterator __f, _InputIterator __l)
  152. { _M_ht.insert_equal(__f,__l); }
  153. #else
  154. void insert(const value_type* __f, const value_type* __l) {
  155. _M_ht.insert_equal(__f,__l);
  156. }
  157. void insert(const_iterator __f, const_iterator __l)
  158. { _M_ht.insert_equal(__f, __l); }
  159. #endif /*__STL_MEMBER_TEMPLATES */
  160. iterator insert_noresize(const value_type& __obj)
  161. { return _M_ht.insert_equal_noresize(__obj); }
  162. //Searches the container for an element with k as key and returns an iterator to it if found,
  163. //otherwise it returns an iterator to hash_multiset::end (the element past the end of the container).
  164. iterator find(const key_type& __key) const { return _M_ht.find(__key); }
  165. //Searches the container for elements with a value of k and returns the number of elements found
  166. size_type count(const key_type& __key) const { return _M_ht.count(__key); }
  167. //Returns the bounds of a range that includes all the elements in the container that compare equal to k.
  168. pair<iterator, iterator> equal_range(const key_type& __key) const
  169. { return _M_ht.equal_range(__key); }
  170. //删除元素
  171. /*
  172. by position (1):
  173. iterator erase ( const_iterator position );
  174. by key (2):
  175. size_type erase ( const key_type& k );
  176. range (3):
  177. iterator erase ( const_iterator first, const_iterator last );
  178. */
  179. size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
  180. void erase(iterator __it) { _M_ht.erase(__it); }
  181. void erase(iterator __f, iterator __l) { _M_ht.erase(__f, __l); }
  182. //清空容器
  183. void clear() { _M_ht.clear(); }
  184. public:
  185. void resize(size_type __hint) { _M_ht.resize(__hint); }
  186. size_type bucket_count() const { return _M_ht.bucket_count(); }
  187. size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
  188. size_type elems_in_bucket(size_type __n) const
  189. { return _M_ht.elems_in_bucket(__n); }};
  190. template <class _Val, class _HashFcn, class _EqualKey, class _Alloc>
  191. inline bool
  192. operator==(const hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs1,
  193. const hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs2){
  194. return __hs1._M_ht == __hs2._M_ht;}
  195. #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
  196. template <class _Val, class _HashFcn, class _EqualKey, class _Alloc>
  197. inline bool
  198. operator!=(const hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs1,
  199. const hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs2) {
  200. return !(__hs1 == __hs2);}
  201. template <class _Val, class _HashFcn, class _EqualKey, class _Alloc>
  202. inline void
  203. swap(hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs1,
  204. hash_multiset<_Val,_HashFcn,_EqualKey,_Alloc>& __hs2) {
  205. __hs1.swap(__hs2);}
  206. #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */