总结

  1. 底层是rb_tree
  2. 插入时使用insert_equal()
  3. 其他操作与map一致

multimap结构

  1. #ifndef __SGI_STL_INTERNAL_MULTIMAP_H
  2. #define __SGI_STL_INTERNAL_MULTIMAP_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. // Forward declaration of operators < and ==, needed for friend declaration.
  10. //multimap的特性及其用法和map完全相同,唯一的区别就是multimap允许键值key重复//因此multimap的插入操作采用的是底层RB-Tree的insert_equal()而非insert_unique()//有关map容器的剖析见前面博文//map内部元素根据键值key默认使用递增排序less//用户可自行制定比较类型//内部维护的数据结构是红黑树, 具有非常优秀的最坏情况的时间复杂度//注意:multimap允许元素重复,即键值和实值都可以重复,这点与map不同
  11. template <class _Key, class _Tp,
  12. class _Compare __STL_DEPENDENT_DEFAULT_TMPL(less<_Key>),
  13. class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >
  14. class multimap;
  15. template <class _Key, class _Tp, class _Compare, class _Alloc>
  16. inline bool operator==(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
  17. const multimap<_Key,_Tp,_Compare,_Alloc>& __y);
  18. template <class _Key, class _Tp, class _Compare, class _Alloc>
  19. inline bool operator<(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
  20. const multimap<_Key,_Tp,_Compare,_Alloc>& __y);
  21. template <class _Key, class _Tp, class _Compare, class _Alloc>
  22. class multimap {
  23. // requirements:
  24. __STL_CLASS_REQUIRES(_Tp, _Assignable);
  25. __STL_CLASS_BINARY_FUNCTION_CHECK(_Compare, bool, _Key, _Key);
  26. public:
  27. // typedefs:
  28. //下面的定义与map相同
  29. typedef _Key key_type;
  30. typedef _Tp data_type;
  31. typedef _Tp mapped_type;
  32. typedef pair<const _Key, _Tp> value_type;
  33. typedef _Compare key_compare;
  34. //嵌套类,提供键值key比较函数接口
  35. //继承自<stl_function.h>中的binary_function
  36. /*
  37. template <class _Arg1, class _Arg2, class _Result>
  38. struct binary_function {
  39. typedef _Arg1 first_argument_type;
  40. typedef _Arg2 second_argument_type;
  41. typedef _Result result_type;
  42. };
  43. */
  44. class value_compare : public binary_function<value_type, value_type, bool> {
  45. friend class multimap<_Key,_Tp,_Compare,_Alloc>;
  46. protected:
  47. _Compare comp;
  48. value_compare(_Compare __c) : comp(__c) {}
  49. public:
  50. bool operator()(const value_type& __x, const value_type& __y) const {
  51. return comp(__x.first, __y.first);
  52. }
  53. };
  54. private:
  55. //底层机制是RB-Tree
  56. typedef _Rb_tree<key_type, value_type,
  57. _Select1st<value_type>, key_compare, _Alloc> _Rep_type;
  58. _Rep_type _M_t; // red-black tree representing multimap
  59. public:
  60. typedef typename _Rep_type::pointer pointer;
  61. typedef typename _Rep_type::const_pointer const_pointer;
  62. typedef typename _Rep_type::reference reference;
  63. typedef typename _Rep_type::const_reference const_reference;
  64. //map的迭代器不直接定义为const_iterator,而是分别定义iterator,const_iterator
  65. //是因为map的键值key不能被修改,因为必须遵守比较函数的排序规则,所以必须定义为const_iterator
  66. //而map的实值value可以被修改,则定义为iterator
  67. typedef typename _Rep_type::iterator iterator;
  68. typedef typename _Rep_type::const_iterator const_iterator;
  69. typedef typename _Rep_type::reverse_iterator reverse_iterator;
  70. typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
  71. typedef typename _Rep_type::size_type size_type;
  72. typedef typename _Rep_type::difference_type difference_type;
  73. typedef typename _Rep_type::allocator_type allocator_type;
  74. // allocation/deallocation
  75. // 注意:multimap只能使用RB-tree的insert-equal(),不能使用insert-unique()
  76. /*
  77. 构造函数
  78. multimap();
  79. explicit multimap (const key_compare& comp = key_compare(),
  80. const allocator_type& alloc = allocator_type());
  81. template <class InputIterator>
  82. multimap (InputIterator first, InputIterator last,
  83. const key_compare& comp = key_compare(),
  84. const allocator_type& alloc = allocator_type());
  85. multimap (const multimap& x);
  86. */
  87. multimap() : _M_t(_Compare(), allocator_type()) { }
  88. explicit multimap(const _Compare& __comp,
  89. const allocator_type& __a = allocator_type())
  90. : _M_t(__comp, __a) { }
  91. #ifdef __STL_MEMBER_TEMPLATES
  92. template <class _InputIterator>
  93. multimap(_InputIterator __first, _InputIterator __last)
  94. : _M_t(_Compare(), allocator_type())
  95. { _M_t.insert_equal(__first, __last); }
  96. template <class _InputIterator>
  97. multimap(_InputIterator __first, _InputIterator __last,
  98. const _Compare& __comp,
  99. const allocator_type& __a = allocator_type())
  100. : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); }
  101. #else
  102. multimap(const value_type* __first, const value_type* __last)
  103. : _M_t(_Compare(), allocator_type())
  104. { _M_t.insert_equal(__first, __last); }
  105. multimap(const value_type* __first, const value_type* __last,
  106. const _Compare& __comp,
  107. const allocator_type& __a = allocator_type())
  108. : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); }
  109. multimap(const_iterator __first, const_iterator __last)
  110. : _M_t(_Compare(), allocator_type())
  111. { _M_t.insert_equal(__first, __last); }
  112. multimap(const_iterator __first, const_iterator __last,
  113. const _Compare& __comp,
  114. const allocator_type& __a = allocator_type())
  115. : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); }
  116. #endif /* __STL_MEMBER_TEMPLATES */
  117. //拷贝构造函数
  118. multimap(const multimap<_Key,_Tp,_Compare,_Alloc>& __x) : _M_t(__x._M_t) { }
  119. //这里提供了operator=,即可以通过=初始化对象
  120. multimap<_Key,_Tp,_Compare,_Alloc>&
  121. operator=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x) {
  122. _M_t = __x._M_t;
  123. return *this;
  124. }
  125. // accessors:
  126. //返回键值的比较函数,这里是调用RB-Tree的key_comp()
  127. key_compare key_comp() const { return _M_t.key_comp(); }
  128. //返回实值的比较函数
  129. //这里调用的是map嵌套类中定义的比较函数
  130. /*
  131. class value_compare
  132. : public binary_function<value_type, value_type, bool> {
  133. friend class map<_Key,_Tp,_Compare,_Alloc>;
  134. protected :
  135. _Compare comp;
  136. value_compare(_Compare __c) : comp(__c) {}
  137. public:
  138. bool operator()(const value_type& __x, const value_type& __y) const {
  139. return comp(__x.first, __y.first);//以键值调用比较函数
  140. }
  141. */
  142. //实际上最终还是调用键值key的比较函数,即他们是调用同一个比较函数
  143. value_compare value_comp() const { return value_compare(_M_t.key_comp()); }
  144. allocator_type get_allocator() const { return _M_t.get_allocator(); }
  145. iterator begin() { return _M_t.begin(); }
  146. const_iterator begin() const { return _M_t.begin(); }
  147. iterator end() { return _M_t.end(); }
  148. const_iterator end() const { return _M_t.end(); }
  149. reverse_iterator rbegin() { return _M_t.rbegin(); }
  150. const_reverse_iterator rbegin() const { return _M_t.rbegin(); }
  151. reverse_iterator rend() { return _M_t.rend(); }
  152. const_reverse_iterator rend() const { return _M_t.rend(); }
  153. //判断容器multimap是否为空
  154. bool empty() const { return _M_t.empty(); }
  155. //返回容器multimap的大小
  156. size_type size() const { return _M_t.size(); }
  157. size_type max_size() const { return _M_t.max_size(); }
  158. //交换multimap对象的内容
  159. void swap(multimap<_Key,_Tp,_Compare,_Alloc>& __x) { _M_t.swap(__x._M_t); }
  160. // insert/erase
  161. /*
  162. multimap只能使用RB-tree的insert-equal()
  163. 插入元素
  164. iterator insert (const value_type& val);
  165. iterator insert (iterator position, const value_type& val);
  166. template <class InputIterator>
  167. void insert (InputIterator first, InputIterator last);
  168. */
  169. //插入元素节点,调用RB-Tree的insert-equal();
  170. //插入元素的键值key允许重复
  171. iterator insert(const value_type& __x) { return _M_t.insert_equal(__x); }
  172. //在指定位置插入元素
  173. iterator insert(iterator __position, const value_type& __x) {
  174. return _M_t.insert_equal(__position, __x);
  175. }
  176. #ifdef __STL_MEMBER_TEMPLATES
  177. //插入[first,last)元素
  178. template <class _InputIterator>
  179. void insert(_InputIterator __first, _InputIterator __last) {
  180. _M_t.insert_equal(__first, __last);
  181. }
  182. #else
  183. void insert(const value_type* __first, const value_type* __last) {
  184. _M_t.insert_equal(__first, __last);
  185. }
  186. void insert(const_iterator __first, const_iterator __last) {
  187. _M_t.insert_equal(__first, __last);
  188. }
  189. #endif /* __STL_MEMBER_TEMPLATES */
  190. /*
  191. 擦除元素
  192. void erase (iterator position);
  193. size_type erase (const key_type& k);
  194. void erase (iterator first, iterator last);
  195. */
  196. //在指定位置擦除元素
  197. void erase(iterator __position) { _M_t.erase(__position); }
  198. //擦除指定键值的节点
  199. size_type erase(const key_type& __x) { return _M_t.erase(__x); }
  200. //擦除指定区间的节点
  201. void erase(iterator __first, iterator __last)
  202. { _M_t.erase(__first, __last); }
  203. //清空容器
  204. void clear() { _M_t.clear(); }
  205. // multimap operations:
  206. //查找指定键值的节点
  207. iterator find(const key_type& __x) { return _M_t.find(__x); }
  208. const_iterator find(const key_type& __x) const { return _M_t.find(__x); }
  209. //计算指定键值元素的个数
  210. size_type count(const key_type& __x) const { return _M_t.count(__x); }
  211. //Returns an iterator pointing to the first element in the container
  212. //whose key is not considered to go before k (i.e., either it is equivalent or goes after).
  213. //this->first is greater than or equivalent to __x.
  214. iterator lower_bound(const key_type& __x) {return _M_t.lower_bound(__x); }
  215. const_iterator lower_bound(const key_type& __x) const {
  216. return _M_t.lower_bound(__x);
  217. }
  218. //Returns an iterator pointing to the first element that is greater than key.
  219. iterator upper_bound(const key_type& __x) {return _M_t.upper_bound(__x); }
  220. const_iterator upper_bound(const key_type& __x) const {
  221. return _M_t.upper_bound(__x);
  222. }
  223. //Returns the bounds of a range that includes all the elements in the container
  224. //which have a key equivalent to k
  225. //Because the elements in a map container have unique keys,
  226. //the range returned will contain a single element at most.
  227. pair<iterator,iterator> equal_range(const key_type& __x) {
  228. return _M_t.equal_range(__x);
  229. }
  230. pair<const_iterator,const_iterator> equal_range(const key_type& __x) const {
  231. return _M_t.equal_range(__x);
  232. }
  233. //以下是操作符重载
  234. #ifdef __STL_TEMPLATE_FRIENDS
  235. template <class _K1, class _T1, class _C1, class _A1>
  236. friend bool operator== (const multimap<_K1, _T1, _C1, _A1>&,
  237. const multimap<_K1, _T1, _C1, _A1>&);
  238. template <class _K1, class _T1, class _C1, class _A1>
  239. friend bool operator< (const multimap<_K1, _T1, _C1, _A1>&,
  240. const multimap<_K1, _T1, _C1, _A1>&);
  241. #else /* __STL_TEMPLATE_FRIENDS */
  242. friend bool __STD_QUALIFIER
  243. operator== __STL_NULL_TMPL_ARGS (const multimap&, const multimap&);
  244. friend bool __STD_QUALIFIER
  245. operator< __STL_NULL_TMPL_ARGS (const multimap&, const multimap&);
  246. #endif /* __STL_TEMPLATE_FRIENDS */};
  247. template <class _Key, class _Tp, class _Compare, class _Alloc>
  248. inline bool operator==(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
  249. const multimap<_Key,_Tp,_Compare,_Alloc>& __y) {
  250. return __x._M_t == __y._M_t;}
  251. template <class _Key, class _Tp, class _Compare, class _Alloc>
  252. inline bool operator<(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
  253. const multimap<_Key,_Tp,_Compare,_Alloc>& __y) {
  254. return __x._M_t < __y._M_t;}
  255. #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
  256. template <class _Key, class _Tp, class _Compare, class _Alloc>
  257. inline bool operator!=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
  258. const multimap<_Key,_Tp,_Compare,_Alloc>& __y) {
  259. return !(__x == __y);}
  260. template <class _Key, class _Tp, class _Compare, class _Alloc>
  261. inline bool operator>(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
  262. const multimap<_Key,_Tp,_Compare,_Alloc>& __y) {
  263. return __y < __x;}
  264. template <class _Key, class _Tp, class _Compare, class _Alloc>
  265. inline bool operator<=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
  266. const multimap<_Key,_Tp,_Compare,_Alloc>& __y) {
  267. return !(__y < __x);}
  268. template <class _Key, class _Tp, class _Compare, class _Alloc>
  269. inline bool operator>=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
  270. const multimap<_Key,_Tp,_Compare,_Alloc>& __y) {
  271. return !(__x < __y);}
  272. template <class _Key, class _Tp, class _Compare, class _Alloc>
  273. inline void swap(multimap<_Key,_Tp,_Compare,_Alloc>& __x,
  274. multimap<_Key,_Tp,_Compare,_Alloc>& __y) {
  275. __x.swap(__y);}
  276. #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
  277. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  278. #pragma reset woff 1174
  279. #pragma reset woff 1375
  280. #endif
  281. __STL_END_NAMESPACE
  282. #endif /* __SGI_STL_INTERNAL_MULTIMAP_H */
  283. // Local Variables:// mode:C++// End: