总结

  1. 底层是hashtable
  2. 相比于hash_map,支持插入重复值
  3. 底层调用的是 insert_equal()

hash_multimap结构

  1. // Forward declaration of equality operator; needed for friend declaration.
  2. //hash_multimap与hash_map的差别就是插入函数,前者的插入函数是采用底层机制hash table的insert_equal()
  3. //后者则采用insert_unique()
  4. //其他的功能都和hash_map类似
  5. //hash_multimap允许key重复
  6. //这里就不再进行注释了,可以参考hash_map的解析
  7. template <class _Key, class _Tp,
  8. class _HashFcn __STL_DEPENDENT_DEFAULT_TMPL(hash<_Key>),
  9. class _EqualKey __STL_DEPENDENT_DEFAULT_TMPL(equal_to<_Key>),
  10. class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >
  11. class hash_multimap;
  12. template <class _Key, class _Tp, class _HF, class _EqKey, class _Alloc>
  13. inline bool
  14. operator==(const hash_multimap<_Key,_Tp,_HF,_EqKey,_Alloc>& __hm1,
  15. const hash_multimap<_Key,_Tp,_HF,_EqKey,_Alloc>& __hm2);
  16. template <class _Key, class _Tp, class _HashFcn, class _EqualKey,
  17. class _Alloc>
  18. class hash_multimap{
  19. // requirements:
  20. __STL_CLASS_REQUIRES(_Key, _Assignable);
  21. __STL_CLASS_REQUIRES(_Tp, _Assignable);
  22. __STL_CLASS_UNARY_FUNCTION_CHECK(_HashFcn, size_t, _Key);
  23. __STL_CLASS_BINARY_FUNCTION_CHECK(_EqualKey, bool, _Key, _Key);
  24. private:
  25. typedef hashtable<pair<const _Key, _Tp>, _Key, _HashFcn,
  26. _Select1st<pair<const _Key, _Tp> >, _EqualKey, _Alloc>
  27. _Ht;
  28. _Ht _M_ht;
  29. public:
  30. typedef typename _Ht::key_type key_type;
  31. typedef _Tp data_type;
  32. typedef _Tp mapped_type;
  33. typedef typename _Ht::value_type value_type;
  34. typedef typename _Ht::hasher hasher;
  35. typedef typename _Ht::key_equal key_equal;
  36. typedef typename _Ht::size_type size_type;
  37. typedef typename _Ht::difference_type difference_type;
  38. typedef typename _Ht::pointer pointer;
  39. typedef typename _Ht::const_pointer const_pointer;
  40. typedef typename _Ht::reference reference;
  41. typedef typename _Ht::const_reference const_reference;
  42. typedef typename _Ht::iterator iterator;
  43. typedef typename _Ht::const_iterator const_iterator;
  44. typedef typename _Ht::allocator_type allocator_type;
  45. hasher hash_funct() const { return _M_ht.hash_funct(); }
  46. key_equal key_eq() const { return _M_ht.key_eq(); }
  47. allocator_type get_allocator() const { return _M_ht.get_allocator(); }
  48. public:
  49. hash_multimap() : _M_ht(100, hasher(), key_equal(), allocator_type()) {}
  50. explicit hash_multimap(size_type __n)
  51. : _M_ht(__n, hasher(), key_equal(), allocator_type()) {}
  52. hash_multimap(size_type __n, const hasher& __hf)
  53. : _M_ht(__n, __hf, key_equal(), allocator_type()) {}
  54. hash_multimap(size_type __n, const hasher& __hf, const key_equal& __eql,
  55. const allocator_type& __a = allocator_type())
  56. : _M_ht(__n, __hf, __eql, __a) {}
  57. #ifdef __STL_MEMBER_TEMPLATES
  58. template <class _InputIterator>
  59. hash_multimap(_InputIterator __f, _InputIterator __l)
  60. : _M_ht(100, hasher(), key_equal(), allocator_type())
  61. { _M_ht.insert_equal(__f, __l); }
  62. template <class _InputIterator>
  63. hash_multimap(_InputIterator __f, _InputIterator __l, size_type __n)
  64. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  65. { _M_ht.insert_equal(__f, __l); }
  66. template <class _InputIterator>
  67. hash_multimap(_InputIterator __f, _InputIterator __l, size_type __n,
  68. const hasher& __hf)
  69. : _M_ht(__n, __hf, key_equal(), allocator_type())
  70. { _M_ht.insert_equal(__f, __l); }
  71. template <class _InputIterator>
  72. hash_multimap(_InputIterator __f, _InputIterator __l, size_type __n,
  73. const hasher& __hf, const key_equal& __eql,
  74. const allocator_type& __a = allocator_type())
  75. : _M_ht(__n, __hf, __eql, __a)
  76. { _M_ht.insert_equal(__f, __l); }
  77. #else
  78. hash_multimap(const value_type* __f, const value_type* __l)
  79. : _M_ht(100, hasher(), key_equal(), allocator_type())
  80. { _M_ht.insert_equal(__f, __l); }
  81. hash_multimap(const value_type* __f, const value_type* __l, size_type __n)
  82. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  83. { _M_ht.insert_equal(__f, __l); }
  84. hash_multimap(const value_type* __f, const value_type* __l, size_type __n,
  85. const hasher& __hf)
  86. : _M_ht(__n, __hf, key_equal(), allocator_type())
  87. { _M_ht.insert_equal(__f, __l); }
  88. hash_multimap(const value_type* __f, const value_type* __l, size_type __n,
  89. const hasher& __hf, const key_equal& __eql,
  90. const allocator_type& __a = allocator_type())
  91. : _M_ht(__n, __hf, __eql, __a)
  92. { _M_ht.insert_equal(__f, __l); }
  93. hash_multimap(const_iterator __f, const_iterator __l)
  94. : _M_ht(100, hasher(), key_equal(), allocator_type())
  95. { _M_ht.insert_equal(__f, __l); }
  96. hash_multimap(const_iterator __f, const_iterator __l, size_type __n)
  97. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  98. { _M_ht.insert_equal(__f, __l); }
  99. hash_multimap(const_iterator __f, const_iterator __l, size_type __n,
  100. const hasher& __hf)
  101. : _M_ht(__n, __hf, key_equal(), allocator_type())
  102. { _M_ht.insert_equal(__f, __l); }
  103. hash_multimap(const_iterator __f, const_iterator __l, size_type __n,
  104. const hasher& __hf, const key_equal& __eql,
  105. const allocator_type& __a = allocator_type())
  106. : _M_ht(__n, __hf, __eql, __a)
  107. { _M_ht.insert_equal(__f, __l); }
  108. #endif /*__STL_MEMBER_TEMPLATES */
  109. public:
  110. size_type size() const { return _M_ht.size(); }
  111. size_type max_size() const { return _M_ht.max_size(); }
  112. bool empty() const { return _M_ht.empty(); }
  113. void swap(hash_multimap& __hs) { _M_ht.swap(__hs._M_ht); }
  114. #ifdef __STL_MEMBER_TEMPLATES
  115. template <class _K1, class _T1, class _HF, class _EqK, class _Al>
  116. friend bool operator== (const hash_multimap<_K1, _T1, _HF, _EqK, _Al>&,
  117. const hash_multimap<_K1, _T1, _HF, _EqK, _Al>&);
  118. #else /* __STL_MEMBER_TEMPLATES */
  119. friend bool __STD_QUALIFIER
  120. operator== __STL_NULL_TMPL_ARGS (const hash_multimap&,const hash_multimap&);
  121. #endif /* __STL_MEMBER_TEMPLATES */
  122. iterator begin() { return _M_ht.begin(); }
  123. iterator end() { return _M_ht.end(); }
  124. const_iterator begin() const { return _M_ht.begin(); }
  125. const_iterator end() const { return _M_ht.end(); }
  126. public:
  127. iterator insert(const value_type& __obj)
  128. { return _M_ht.insert_equal(__obj); }
  129. #ifdef __STL_MEMBER_TEMPLATES
  130. template <class _InputIterator>
  131. void insert(_InputIterator __f, _InputIterator __l)
  132. { _M_ht.insert_equal(__f,__l); }
  133. #else
  134. void insert(const value_type* __f, const value_type* __l) {
  135. _M_ht.insert_equal(__f,__l);
  136. }
  137. void insert(const_iterator __f, const_iterator __l)
  138. { _M_ht.insert_equal(__f, __l); }
  139. #endif /*__STL_MEMBER_TEMPLATES */
  140. iterator insert_noresize(const value_type& __obj)
  141. { return _M_ht.insert_equal_noresize(__obj); }
  142. iterator find(const key_type& __key) { return _M_ht.find(__key); }
  143. const_iterator find(const key_type& __key) const
  144. { return _M_ht.find(__key); }
  145. size_type count(const key_type& __key) const { return _M_ht.count(__key); }
  146. pair<iterator, iterator> equal_range(const key_type& __key)
  147. { return _M_ht.equal_range(__key); }
  148. pair<const_iterator, const_iterator>
  149. equal_range(const key_type& __key) const
  150. { return _M_ht.equal_range(__key); }
  151. size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
  152. void erase(iterator __it) { _M_ht.erase(__it); }
  153. void erase(iterator __f, iterator __l) { _M_ht.erase(__f, __l); }
  154. void clear() { _M_ht.clear(); }
  155. public:
  156. void resize(size_type __hint) { _M_ht.resize(__hint); }
  157. size_type bucket_count() const { return _M_ht.bucket_count(); }
  158. size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
  159. size_type elems_in_bucket(size_type __n) const
  160. { return _M_ht.elems_in_bucket(__n); }};
  161. template <class _Key, class _Tp, class _HF, class _EqKey, class _Alloc>
  162. inline bool
  163. operator==(const hash_multimap<_Key,_Tp,_HF,_EqKey,_Alloc>& __hm1,
  164. const hash_multimap<_Key,_Tp,_HF,_EqKey,_Alloc>& __hm2){
  165. return __hm1._M_ht == __hm2._M_ht;}
  166. #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
  167. template <class _Key, class _Tp, class _HF, class _EqKey, class _Alloc>
  168. inline bool
  169. operator!=(const hash_multimap<_Key,_Tp,_HF,_EqKey,_Alloc>& __hm1,
  170. const hash_multimap<_Key,_Tp,_HF,_EqKey,_Alloc>& __hm2) {
  171. return !(__hm1 == __hm2);}
  172. template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc>
  173. inline void
  174. swap(hash_multimap<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm1,
  175. hash_multimap<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm2){
  176. __hm1.swap(__hm2);}
  177. #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
  178. // Specialization of insert_iterator so that it will work for hash_map// and hash_multimap.
  179. #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
  180. template <class _Key, class _Tp, class _HashFn, class _EqKey, class _Alloc>
  181. class insert_iterator<hash_map<_Key, _Tp, _HashFn, _EqKey, _Alloc> > {
  182. protected:
  183. typedef hash_map<_Key, _Tp, _HashFn, _EqKey, _Alloc> _Container;
  184. _Container* container;
  185. public:
  186. typedef _Container container_type;
  187. typedef output_iterator_tag iterator_category;
  188. typedef void value_type;
  189. typedef void difference_type;
  190. typedef void pointer;
  191. typedef void reference;
  192. insert_iterator(_Container& __x) : container(&__x) {}
  193. insert_iterator(_Container& __x, typename _Container::iterator)
  194. : container(&__x) {}
  195. insert_iterator<_Container>&
  196. operator=(const typename _Container::value_type& __value) {
  197. container->insert(__value);
  198. return *this;
  199. }
  200. insert_iterator<_Container>& operator*() { return *this; }
  201. insert_iterator<_Container>& operator++() { return *this; }
  202. insert_iterator<_Container>& operator++(int) { return *this; }};
  203. template <class _Key, class _Tp, class _HashFn, class _EqKey, class _Alloc>
  204. class insert_iterator<hash_multimap<_Key, _Tp, _HashFn, _EqKey, _Alloc> > {
  205. protected:
  206. typedef hash_multimap<_Key, _Tp, _HashFn, _EqKey, _Alloc> _Container;
  207. _Container* container;
  208. typename _Container::iterator iter;
  209. public:
  210. typedef _Container container_type;
  211. typedef output_iterator_tag iterator_category;
  212. typedef void value_type;
  213. typedef void difference_type;
  214. typedef void pointer;
  215. typedef void reference;
  216. insert_iterator(_Container& __x) : container(&__x) {}
  217. insert_iterator(_Container& __x, typename _Container::iterator)
  218. : container(&__x) {}
  219. insert_iterator<_Container>&
  220. operator=(const typename _Container::value_type& __value) {
  221. container->insert(__value);
  222. return *this;
  223. }
  224. insert_iterator<_Container>& operator*() { return *this; }
  225. insert_iterator<_Container>& operator++() { return *this; }
  226. insert_iterator<_Container>& operator++(int) { return *this; }};
  227. #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
  228. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  229. #pragma reset woff 1174
  230. #pragma reset woff 1375
  231. #endif
  232. __STL_END_NAMESPACE
  233. #endif /* __SGI_STL_INTERNAL_HASH_MAP_H */
  234. // Local Variables:// mode:C++// End: