总结

  1. 底层是hashtable
  2. 需要以 pair 形式插入
  3. 正向迭代器,即只支持++p操作,不允许p+=n这种随机访问
  4. unordered_map如果要支持pair作为key,那么需要自定义hash函数。而map是支持pair作为key的,因此如果在数据量不大的情况下,为了方便可以使用map作为容器 ```cpp struct pair_hash { template std::size_t operator() (const pair& p) const {
    1. auto h1 = std::hash<T1>{}(p.first); // 这里的{}的意思是实例化一个临时的hash对象,然后调用该对对象的operator()方法.
    2. auto h2 = std::hash<T2>{}(p.second); // 等价于std::hash<T2>()(p.second), 也等价于std::hash<T2> t; t(p.second);
    3. return h1 ^ h2;
    } };

int main() { unordered_map, int, pair_hash> myMap; myMap[{1, 1}] = 1; myMap[{2, 2}] = 2; myMap[{2, 1}] = 3; myMap[{1, 2}] = 4; for (auto& kv: myMap) { std::cout << kv.first.first << “ “ << kv.first.second << “ “ << kv.second << std::endl; }

// map示例 map, int> myMap2; // map可以直接把pair当作key,而且他可以接收的是自定义的排序算法,而不是hash算法 myMap2[{1, 1}] = 1; myMap2[{2, 2}] = 2; myMap2[{2, 1}] = 3; myMap2[{1, 2}] = 4; for (auto& kv: myMap2) { std::cout << kv.first.first << “ “ << kv.first.second << “ “ << kv.second << std::endl; }

}

  1. <a name="PeVYj"></a>
  2. ## hash_map结构
  3. ```cpp
  4. #ifndef __SGI_STL_INTERNAL_HASH_MAP_H
  5. #define __SGI_STL_INTERNAL_HASH_MAP_H
  6. #include <concept_checks.h>
  7. __STL_BEGIN_NAMESPACE
  8. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  9. #pragma set woff 1174
  10. #pragma set woff 1375
  11. #endif
  12. //hash_map的底层是基于hash table的,hash table没有提供排序,所以hash_map容器里面的内容是没排序的
  13. // Forward declaration of equality operator; needed for friend declaration.
  14. template <class _Key, class _Tp,
  15. class _HashFcn __STL_DEPENDENT_DEFAULT_TMPL(hash<_Key>),
  16. class _EqualKey __STL_DEPENDENT_DEFAULT_TMPL(equal_to<_Key>),
  17. class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >
  18. class hash_map;
  19. template <class _Key, class _Tp, class _HashFn, class _EqKey, class _Alloc>
  20. inline bool operator==(const hash_map<_Key, _Tp, _HashFn, _EqKey, _Alloc>&,
  21. const hash_map<_Key, _Tp, _HashFn, _EqKey, _Alloc>&);
  22. template <class _Key, class _Tp, class _HashFcn, class _EqualKey,
  23. class _Alloc>
  24. class hash_map{
  25. // requirements:
  26. __STL_CLASS_REQUIRES(_Key, _Assignable);
  27. __STL_CLASS_REQUIRES(_Tp, _Assignable);
  28. __STL_CLASS_UNARY_FUNCTION_CHECK(_HashFcn, size_t, _Key);
  29. __STL_CLASS_BINARY_FUNCTION_CHECK(_EqualKey, bool, _Key, _Key);
  30. private:
  31. //_Select1st<>取出键值key,_Select1st<>定义在<stl_function.h>
  32. /*
  33. template <class _Arg, class _Result>
  34. struct unary_function {
  35. typedef _Arg argument_type;
  36. typedef _Result result_type;
  37. };
  38. template <class _Pair>
  39. struct _Select1st : public unary_function<_Pair, typename _Pair::first_type> {
  40. const typename _Pair::first_type& operator()(const _Pair& __x) const {
  41. return __x.first;
  42. }
  43. };
  44. */
  45. typedef hashtable<pair<const _Key,_Tp>,_Key,_HashFcn,
  46. _Select1st<pair<const _Key,_Tp> >,_EqualKey,_Alloc> _Ht;
  47. _Ht _M_ht;//底层机制以hash table完成
  48. public:
  49. //以下的内嵌类型均来是hash table
  50. typedef typename _Ht::key_type key_type;
  51. typedef _Tp data_type;
  52. typedef _Tp mapped_type;
  53. typedef typename _Ht::value_type value_type;
  54. typedef typename _Ht::hasher hasher;
  55. typedef typename _Ht::key_equal key_equal;
  56. typedef typename _Ht::size_type size_type;
  57. typedef typename _Ht::difference_type difference_type;
  58. typedef typename _Ht::pointer pointer;
  59. typedef typename _Ht::const_pointer const_pointer;
  60. typedef typename _Ht::reference reference;
  61. typedef typename _Ht::const_reference const_reference;
  62. typedef typename _Ht::iterator iterator;
  63. typedef typename _Ht::const_iterator const_iterator;
  64. typedef typename _Ht::allocator_type allocator_type;
  65. //返回hash相关函数
  66. hasher hash_funct() const { return _M_ht.hash_funct(); }
  67. key_equal key_eq() const { return _M_ht.key_eq(); }
  68. allocator_type get_allocator() const { return _M_ht.get_allocator(); }
  69. public://构造函数//缺省情况使用大小为100,但是实际分配的空间大小为不小于100的最小素数//只是空的hash_map,不存储元素节点
  70. hash_map() : _M_ht(100, hasher(), key_equal(), allocator_type()) {}
  71. //指定大小n的hash_map表
  72. explicit hash_map(size_type __n)
  73. : _M_ht(__n, hasher(), key_equal(), allocator_type()) {}
  74. //指定大小为n,且指定hash函数的hash_map
  75. hash_map(size_type __n, const hasher& __hf)
  76. : _M_ht(__n, __hf, key_equal(), allocator_type()) {}
  77. //指定大小为n,且指定hash函数和键值比较函数的hash_map
  78. hash_map(size_type __n, const hasher& __hf, const key_equal& __eql,
  79. const allocator_type& __a = allocator_type())
  80. : _M_ht(__n, __hf, __eql, __a) {}
  81. #ifdef __STL_MEMBER_TEMPLATES
  82. //以下hash_map的插入操作使用hash table的insert_unique插入
  83. //不允许有相同的键值插入
  84. //用某个范围的元素初始化hash_map对象
  85. //相当于把某个范围[f,l)插入到空的hash_map
  86. template <class _InputIterator>
  87. hash_map(_InputIterator __f, _InputIterator __l)
  88. : _M_ht(100, hasher(), key_equal(), allocator_type())
  89. { _M_ht.insert_unique(__f, __l); }//调用hash table的插入函数
  90. template <class _InputIterator>
  91. hash_map(_InputIterator __f, _InputIterator __l, size_type __n)
  92. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  93. { _M_ht.insert_unique(__f, __l); }
  94. template <class _InputIterator>
  95. hash_map(_InputIterator __f, _InputIterator __l, size_type __n,
  96. const hasher& __hf)
  97. : _M_ht(__n, __hf, key_equal(), allocator_type())
  98. { _M_ht.insert_unique(__f, __l); }
  99. template <class _InputIterator>
  100. hash_map(_InputIterator __f, _InputIterator __l, size_type __n,
  101. const hasher& __hf, const key_equal& __eql,
  102. const allocator_type& __a = allocator_type())
  103. : _M_ht(__n, __hf, __eql, __a)
  104. { _M_ht.insert_unique(__f, __l); }
  105. #else
  106. hash_map(const value_type* __f, const value_type* __l)
  107. : _M_ht(100, hasher(), key_equal(), allocator_type())
  108. { _M_ht.insert_unique(__f, __l); }
  109. hash_map(const value_type* __f, const value_type* __l, size_type __n)
  110. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  111. { _M_ht.insert_unique(__f, __l); }
  112. hash_map(const value_type* __f, const value_type* __l, size_type __n,
  113. const hasher& __hf)
  114. : _M_ht(__n, __hf, key_equal(), allocator_type())
  115. { _M_ht.insert_unique(__f, __l); }
  116. hash_map(const value_type* __f, const value_type* __l, size_type __n,
  117. const hasher& __hf, const key_equal& __eql,
  118. const allocator_type& __a = allocator_type())
  119. : _M_ht(__n, __hf, __eql, __a)
  120. { _M_ht.insert_unique(__f, __l); }
  121. hash_map(const_iterator __f, const_iterator __l)
  122. : _M_ht(100, hasher(), key_equal(), allocator_type())
  123. { _M_ht.insert_unique(__f, __l); }
  124. hash_map(const_iterator __f, const_iterator __l, size_type __n)
  125. : _M_ht(__n, hasher(), key_equal(), allocator_type())
  126. { _M_ht.insert_unique(__f, __l); }
  127. hash_map(const_iterator __f, const_iterator __l, size_type __n,
  128. const hasher& __hf)
  129. : _M_ht(__n, __hf, key_equal(), allocator_type())
  130. { _M_ht.insert_unique(__f, __l); }
  131. hash_map(const_iterator __f, const_iterator __l, size_type __n,
  132. const hasher& __hf, const key_equal& __eql,
  133. const allocator_type& __a = allocator_type())
  134. : _M_ht(__n, __hf, __eql, __a)
  135. { _M_ht.insert_unique(__f, __l); }
  136. #endif /*__STL_MEMBER_TEMPLATES */
  137. public://以下的函数操作只是调用hash table的成员函数//返回 hash_map 容器中元素的个数.
  138. size_type size() const { return _M_ht.size(); }//返回hash_map容器最大存储元素的个数.
  139. size_type max_size() const { return _M_ht.max_size(); }//Returns a bool value indicating whether the hash_map container is empty,//i.e. whether its size is 0.
  140. bool empty() const { return _M_ht.empty(); }
  141. //交换两个存储相同元素类型的hash_map容器内容,但是hash_map容器大小可以不同
  142. void swap(hash_map& __hs) { _M_ht.swap(__hs._M_ht); }
  143. #ifdef __STL_MEMBER_TEMPLATES
  144. template <class _K1, class _T1, class _HF, class _EqK, class _Al>
  145. friend bool operator== (const hash_map<_K1, _T1, _HF, _EqK, _Al>&,
  146. const hash_map<_K1, _T1, _HF, _EqK, _Al>&);
  147. #else /* __STL_MEMBER_TEMPLATES */
  148. friend bool __STD_QUALIFIER
  149. operator== __STL_NULL_TMPL_ARGS (const hash_map&, const hash_map&);
  150. #endif /* __STL_MEMBER_TEMPLATES */
  151. iterator begin() { return _M_ht.begin(); }
  152. iterator end() { return _M_ht.end(); }
  153. const_iterator begin() const { return _M_ht.begin(); }
  154. const_iterator end() const { return _M_ht.end(); }
  155. public:
  156. //插入元素
  157. /*
  158. (1)
  159. pair<iterator,bool> insert ( const value_type& val );
  160. (2)
  161. template <class InputIterator>
  162. void insert ( InputIterator first, InputIterator last );
  163. */
  164. //不允许有重复的键值
  165. //返回pair第二个参数second若为true则插入成功
  166. pair<iterator,bool> insert(const value_type& __obj)
  167. { return _M_ht.insert_unique(__obj); }//调用hash table的insert_unique()函数
  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. { return _M_ht.insert_unique_noresize(__obj); }
  181. //Searches the container for an element with k as key and returns an iterator to it if found,
  182. //otherwise it returns an iterator to hash_map::end
  183. iterator find(const key_type& __key) { return _M_ht.find(__key); }
  184. const_iterator find(const key_type& __key) const
  185. { return _M_ht.find(__key); }
  186. //If k matches the key of an element in the container, the function returns a reference to its mapped value.
  187. _Tp& operator[](const key_type& __key) {
  188. return _M_ht.find_or_insert(value_type(__key, _Tp())).second;
  189. }
  190. //Searches the container for elements whose key is k and returns the number of elements found.
  191. //由于不存在重复的键值,所以返回的个数最多为1个
  192. size_type count(const key_type& __key) const { return _M_ht.count(__key); }
  193. //Returns the bounds of a range that includes all the elements in the container with a key that compares equal to k
  194. //由于不存在重复的键值,所以返回的元素最多为1个
  195. pair<iterator, iterator> equal_range(const key_type& __key)
  196. { return _M_ht.equal_range(__key); }
  197. pair<const_iterator, const_iterator>
  198. equal_range(const key_type& __key) const
  199. { return _M_ht.equal_range(__key); }
  200. //删除元素
  201. /*
  202. by position (1)
  203. iterator erase ( const_iterator position );
  204. by key (2)
  205. size_type erase ( const key_type& k );
  206. range (3)
  207. iterator erase ( const_iterator first, const_iterator last );
  208. */
  209. //擦除指定键值的元素,并返回擦除的个数
  210. //因为键值唯一,则该键值的元素最多为1个
  211. size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
  212. //擦除指定位置的元素
  213. void erase(iterator __it) { _M_ht.erase(__it); }
  214. //擦除指定范围的元素
  215. void erase(iterator __f, iterator __l) { _M_ht.erase(__f, __l); }
  216. //清空hash_map容器
  217. void clear() { _M_ht.clear(); }
  218. //调整hash_set容器的容量
  219. void resize(size_type __hint) { _M_ht.resize(__hint); }
  220. //Returns the number of buckets in the hash_map container.
  221. size_type bucket_count() const { return _M_ht.bucket_count(); }
  222. //Returns the maximum number of buckets that the hash_map container can have.
  223. size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
  224. //Returns the number of elements in bucket n
  225. size_type elems_in_bucket(size_type __n) const
  226. { return _M_ht.elems_in_bucket(__n); }//返回指定桶子键值key中list链表的元素个数};
  227. template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc>
  228. inline bool
  229. operator==(const hash_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm1,
  230. const hash_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm2){
  231. return __hm1._M_ht == __hm2._M_ht;}
  232. #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
  233. template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc>
  234. inline bool
  235. operator!=(const hash_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm1,
  236. const hash_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm2) {
  237. return !(__hm1 == __hm2);}
  238. //交换两个hash_map容器的内容
  239. template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc>
  240. inline void
  241. swap(hash_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm1,
  242. hash_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm2){
  243. __hm1.swap(__hm2);}
  244. #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */