总结

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

multiset结构

  1. #ifndef __SGI_STL_INTERNAL_MULTISET_H
  2. #define __SGI_STL_INTERNAL_MULTISET_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. //multiset的特性及其用法和set完全相同,唯一的区别就是multiset允许键值key重复//因此multiset的插入操作采用的是底层RB-Tree的insert_equal()而非insert_unique()//有关set容器的剖析见前面博文
  10. // Forward declaration of operators < and ==, needed for friend declaration.
  11. //multiset内部元素默认使用递增排序less//用户可自行制定比较类型//内部维护的数据结构是红黑树, 具有非常优秀的最坏情况的时间复杂度
  12. template <class _Key, class _Compare __STL_DEPENDENT_DEFAULT_TMPL(less<_Key>),
  13. class _Alloc = __STL_DEFAULT_ALLOCATOR(_Key) >
  14. class multiset;
  15. template <class _Key, class _Compare, class _Alloc>
  16. inline bool operator==(const multiset<_Key,_Compare,_Alloc>& __x,
  17. const multiset<_Key,_Compare,_Alloc>& __y);
  18. template <class _Key, class _Compare, class _Alloc>
  19. inline bool operator<(const multiset<_Key,_Compare,_Alloc>& __x,
  20. const multiset<_Key,_Compare,_Alloc>& __y);
  21. template <class _Key, class _Compare, class _Alloc>
  22. class multiset {
  23. // requirements:
  24. __STL_CLASS_REQUIRES(_Key, _Assignable);
  25. __STL_CLASS_BINARY_FUNCTION_CHECK(_Compare, bool, _Key, _Key);
  26. public:
  27. // typedefs:
  28. //以下的定义与set相同
  29. typedef _Key key_type;
  30. typedef _Key value_type;
  31. typedef _Compare key_compare;
  32. typedef _Compare value_compare;
  33. private:
  34. //底层机制是采用RB-Tree
  35. typedef _Rb_tree<key_type, value_type,
  36. _Identity<value_type>, key_compare, _Alloc> _Rep_type;
  37. _Rep_type _M_t; // red-black tree representing multiset
  38. public:
  39. typedef typename _Rep_type::const_pointer pointer;
  40. typedef typename _Rep_type::const_pointer const_pointer;
  41. typedef typename _Rep_type::const_reference reference;
  42. typedef typename _Rep_type::const_reference const_reference;
  43. typedef typename _Rep_type::const_iterator iterator;
  44. typedef typename _Rep_type::const_iterator const_iterator;
  45. typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
  46. typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
  47. typedef typename _Rep_type::size_type size_type;
  48. typedef typename _Rep_type::difference_type difference_type;
  49. typedef typename _Rep_type::allocator_type allocator_type;
  50. // allocation/deallocation
  51. /*
  52. 构造函数
  53. multiset();
  54. explicit multiset (const key_compare& comp = key_compare(),
  55. const allocator_type& alloc = allocator_type());
  56. template <class InputIterator>
  57. multiset (InputIterator first, InputIterator last,
  58. const key_compare& comp = key_compare(),
  59. const allocator_type& alloc = allocator_type());
  60. multiset (const multiset& x);
  61. */
  62. multiset() : _M_t(_Compare(), allocator_type()) {}
  63. explicit multiset(const _Compare& __comp,
  64. const allocator_type& __a = allocator_type())
  65. : _M_t(__comp, __a) {}
  66. #ifdef __STL_MEMBER_TEMPLATES
  67. //multiset的插入操作采用的是底层RB-Tree的insert_equal()而非insert_unique()
  68. template <class _InputIterator>
  69. multiset(_InputIterator __first, _InputIterator __last)
  70. : _M_t(_Compare(), allocator_type())
  71. { _M_t.insert_equal(__first, __last); }
  72. template <class _InputIterator>
  73. multiset(_InputIterator __first, _InputIterator __last,
  74. const _Compare& __comp,
  75. const allocator_type& __a = allocator_type())
  76. : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); }
  77. #else
  78. multiset(const value_type* __first, const value_type* __last)
  79. : _M_t(_Compare(), allocator_type())
  80. { _M_t.insert_equal(__first, __last); }
  81. multiset(const value_type* __first, const value_type* __last,
  82. const _Compare& __comp,
  83. const allocator_type& __a = allocator_type())
  84. : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); }
  85. multiset(const_iterator __first, const_iterator __last)
  86. : _M_t(_Compare(), allocator_type())
  87. { _M_t.insert_equal(__first, __last); }
  88. multiset(const_iterator __first, const_iterator __last,
  89. const _Compare& __comp,
  90. const allocator_type& __a = allocator_type())
  91. : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); }
  92. #endif /* __STL_MEMBER_TEMPLATES */
  93. multiset(const multiset<_Key,_Compare,_Alloc>& __x) : _M_t(__x._M_t) {}
  94. multiset<_Key,_Compare,_Alloc>&
  95. operator=(const multiset<_Key,_Compare,_Alloc>& __x) {
  96. _M_t = __x._M_t;//调用了底层红黑树的operator=操作函数
  97. return *this;
  98. }
  99. //以下所有的multiset操作行为,RB-tree都已提供,所以multiset只要调用即可
  100. // accessors:
  101. //返回用于key比较的函数,调用RB-Tree的key_comp()
  102. key_compare key_comp() const { return _M_t.key_comp(); }
  103. //由于multiset的性质, value和key使用同一个比较函数
  104. value_compare value_comp() const { return _M_t.key_comp(); }
  105. allocator_type get_allocator() const { return _M_t.get_allocator(); }
  106. iterator begin() const { return _M_t.begin(); }
  107. iterator end() const { return _M_t.end(); }
  108. reverse_iterator rbegin() const { return _M_t.rbegin(); }
  109. reverse_iterator rend() const { return _M_t.rend(); }
  110. bool empty() const { return _M_t.empty(); }
  111. size_type size() const { return _M_t.size(); }
  112. size_type max_size() const { return _M_t.max_size(); }
  113. //这里调用的swap()函数是专属于RB-Tree的swap(),并不是STL的swap()算法
  114. void swap(multiset<_Key,_Compare,_Alloc>& __x) { _M_t.swap(__x._M_t); }
  115. // insert/erase
  116. /*
  117. 插入节点
  118. iterator insert (const value_type& val);
  119. iterator insert (iterator position, const value_type& val);
  120. template <class InputIterator>
  121. void insert (InputIterator first, InputIterator last);
  122. */
  123. //插入数据节点
  124. iterator insert(const value_type& __x) {
  125. return _M_t.insert_equal(__x);
  126. }
  127. //在指定位置插入节点
  128. iterator insert(iterator __position, const value_type& __x) {
  129. typedef typename _Rep_type::iterator _Rep_iterator;
  130. return _M_t.insert_equal((_Rep_iterator&)__position, __x);
  131. }
  132. #ifdef __STL_MEMBER_TEMPLATES
  133. template <class _InputIterator>
  134. void insert(_InputIterator __first, _InputIterator __last) {
  135. _M_t.insert_equal(__first, __last);
  136. }
  137. #else
  138. void insert(const value_type* __first, const value_type* __last) {
  139. _M_t.insert_equal(__first, __last);
  140. }
  141. void insert(const_iterator __first, const_iterator __last) {
  142. _M_t.insert_equal(__first, __last);
  143. }
  144. #endif /* __STL_MEMBER_TEMPLATES */
  145. //擦除指定位置的元素
  146. void erase(iterator __position) {
  147. typedef typename _Rep_type::iterator _Rep_iterator;
  148. _M_t.erase((_Rep_iterator&)__position);
  149. }
  150. //擦除元素值为x的节点
  151. size_type erase(const key_type& __x) {
  152. return _M_t.erase(__x);
  153. }
  154. //擦除指定区间的节点
  155. void erase(iterator __first, iterator __last) {
  156. typedef typename _Rep_type::iterator _Rep_iterator;
  157. _M_t.erase((_Rep_iterator&)__first, (_Rep_iterator&)__last);
  158. }
  159. //清除multiset
  160. void clear() { _M_t.clear(); }
  161. // multiset operations:
  162. //查找元素值为x的节点
  163. iterator find(const key_type& __x) const { return _M_t.find(__x); }
  164. //返回指定元素的个数
  165. size_type count(const key_type& __x) const { return _M_t.count(__x); }
  166. //Returns an iterator pointing to the first element in the container
  167. //whose key is not considered to go before k (i.e., either it is equivalent or goes after).
  168. iterator lower_bound(const key_type& __x) const {
  169. return _M_t.lower_bound(__x);
  170. }
  171. //Returns an iterator pointing to the first element that is greater than key.
  172. iterator upper_bound(const key_type& __x) const {
  173. return _M_t.upper_bound(__x);
  174. }
  175. //Returns the bounds of a range that includes all the elements in the container
  176. //which have a key equivalent to k
  177. //Because the elements in a map container have unique keys,
  178. //the range returned will contain a single element at most.
  179. pair<iterator,iterator> equal_range(const key_type& __x) const {
  180. return _M_t.equal_range(__x);
  181. }
  182. //以下是操作符的重载
  183. #ifdef __STL_TEMPLATE_FRIENDS
  184. template <class _K1, class _C1, class _A1>
  185. friend bool operator== (const multiset<_K1,_C1,_A1>&,
  186. const multiset<_K1,_C1,_A1>&);
  187. template <class _K1, class _C1, class _A1>
  188. friend bool operator< (const multiset<_K1,_C1,_A1>&,
  189. const multiset<_K1,_C1,_A1>&);
  190. #else /* __STL_TEMPLATE_FRIENDS */
  191. friend bool __STD_QUALIFIER
  192. operator== __STL_NULL_TMPL_ARGS (const multiset&, const multiset&);
  193. friend bool __STD_QUALIFIER
  194. operator< __STL_NULL_TMPL_ARGS (const multiset&, const multiset&);
  195. #endif /* __STL_TEMPLATE_FRIENDS */};
  196. template <class _Key, class _Compare, class _Alloc>
  197. inline bool operator==(const multiset<_Key,_Compare,_Alloc>& __x,
  198. const multiset<_Key,_Compare,_Alloc>& __y) {
  199. return __x._M_t == __y._M_t;}
  200. template <class _Key, class _Compare, class _Alloc>
  201. inline bool operator<(const multiset<_Key,_Compare,_Alloc>& __x,
  202. const multiset<_Key,_Compare,_Alloc>& __y) {
  203. return __x._M_t < __y._M_t;}
  204. #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
  205. template <class _Key, class _Compare, class _Alloc>
  206. inline bool operator!=(const multiset<_Key,_Compare,_Alloc>& __x,
  207. const multiset<_Key,_Compare,_Alloc>& __y) {
  208. return !(__x == __y);}
  209. template <class _Key, class _Compare, class _Alloc>
  210. inline bool operator>(const multiset<_Key,_Compare,_Alloc>& __x,
  211. const multiset<_Key,_Compare,_Alloc>& __y) {
  212. return __y < __x;}
  213. template <class _Key, class _Compare, class _Alloc>
  214. inline bool operator<=(const multiset<_Key,_Compare,_Alloc>& __x,
  215. const multiset<_Key,_Compare,_Alloc>& __y) {
  216. return !(__y < __x);}
  217. template <class _Key, class _Compare, class _Alloc>
  218. inline bool operator>=(const multiset<_Key,_Compare,_Alloc>& __x,
  219. const multiset<_Key,_Compare,_Alloc>& __y) {
  220. return !(__x < __y);}
  221. template <class _Key, class _Compare, class _Alloc>
  222. inline void swap(multiset<_Key,_Compare,_Alloc>& __x,
  223. multiset<_Key,_Compare,_Alloc>& __y) {
  224. __x.swap(__y);}
  225. #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
  226. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  227. #pragma reset woff 1174
  228. #pragma reset woff 1375
  229. #endif
  230. __STL_END_NAMESPACE
  231. #endif /* __SGI_STL_INTERNAL_MULTISET_H */
  232. // Local Variables:// mode:C++// End: