总结

  1. 底层实现是 rb_tree,因此是有序的,即会按照key的值从小到大排序(默认less函数)
  2. 主要操作API有:insert、empty、size、erase、find;insert返回的pair结构,pair 当插入成功 bool 为true,iterator为对应的迭代器
  3. 如果把pair作为key,那么需要自己实现比较函数
  4. 底层也有key和value参数,不过value和key是一样的
  5. 因为要保持唯一,因此调用的rb_tree的 insert_unique
  6. 插入值的方法时insert,没有push,push方法对应的容器都是有特性的,比如stack、queue、priority_queue,而push_back/push_front则是对应vector/deque

set结构

  1. #ifndef __SGI_STL_INTERNAL_SET_H
  2. #define __SGI_STL_INTERNAL_SET_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. //set内部元素默认使用递增排序less//用户可自行制定比较类型//内部维护的数据结构是红黑树, 具有非常优秀的最坏情况的时间复杂度 //注意: set键值和实值是相同的,set内不允许重复元素的存在, 如果插入重复元素,则会忽略插入操作 //且不能通过迭代器修改set的元素值,其迭代器类型是定义为RB-Tree的const_iterator
  10. // Forward declarations of operators < and ==, needed for friend declaration.
  11. template <class _Key, class _Compare __STL_DEPENDENT_DEFAULT_TMPL(less<_Key>),
  12. class _Alloc = __STL_DEFAULT_ALLOCATOR(_Key) >
  13. class set;
  14. template <class _Key, class _Compare, class _Alloc>
  15. inline bool operator==(const set<_Key,_Compare,_Alloc>& __x,
  16. const set<_Key,_Compare,_Alloc>& __y);
  17. template <class _Key, class _Compare, class _Alloc>
  18. inline bool operator<(const set<_Key,_Compare,_Alloc>& __x,
  19. const set<_Key,_Compare,_Alloc>& __y);
  20. template <class _Key, class _Compare, class _Alloc>
  21. class set {
  22. // requirements:
  23. __STL_CLASS_REQUIRES(_Key, _Assignable);
  24. __STL_CLASS_BINARY_FUNCTION_CHECK(_Compare, bool, _Key, _Key);
  25. public:
  26. // typedefs:
  27. //在set中key就是value, value同时也是key
  28. typedef _Key key_type;
  29. typedef _Key value_type;
  30. //注意:以下key_compare和value_compare使用相同的比较函数
  31. typedef _Compare key_compare;
  32. typedef _Compare value_compare;
  33. private:
  34. //set的底层机制是采用RB-Tree数据结构,在<stl_tree.h>实现
  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 set
  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. //set的迭代器iterator 定义为RB-Tree的const_iterator,不允许用户通过迭代器修改set的元素值
  44. //因为set的元素有一定次序安排,修改其值会破坏排序规则
  45. typedef typename _Rep_type::const_iterator iterator;
  46. typedef typename _Rep_type::const_iterator const_iterator;
  47. typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
  48. typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
  49. typedef typename _Rep_type::size_type size_type;
  50. typedef typename _Rep_type::difference_type difference_type;
  51. typedef typename _Rep_type::allocator_type allocator_type;
  52. // allocation/deallocation
  53. // set只能使用RB-tree的insert-unique(),不能使用insert-equal()
  54. //当要插入键值和已经存在的键值相同时,就会被忽略
  55. /*构造函数原型
  56. set();
  57. explicit set (const key_compare& comp = key_compare(),
  58. const allocator_type& alloc = allocator_type());
  59. template <class InputIterator>
  60. set (InputIterator first, InputIterator last,
  61. const key_compare& comp = key_compare(),
  62. const allocator_type& alloc = allocator_type());
  63. set (const set& x);
  64. */
  65. set() : _M_t(_Compare(), allocator_type()) {}
  66. explicit set(const _Compare& __comp,
  67. const allocator_type& __a = allocator_type())
  68. : _M_t(__comp, __a) {}
  69. #ifdef __STL_MEMBER_TEMPLATES
  70. template <class _InputIterator>
  71. set(_InputIterator __first, _InputIterator __last)
  72. : _M_t(_Compare(), allocator_type())
  73. { _M_t.insert_unique(__first, __last); }
  74. template <class _InputIterator>
  75. set(_InputIterator __first, _InputIterator __last, const _Compare& __comp,
  76. const allocator_type& __a = allocator_type())
  77. : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
  78. #else
  79. set(const value_type* __first, const value_type* __last)
  80. : _M_t(_Compare(), allocator_type())
  81. { _M_t.insert_unique(__first, __last); }
  82. set(const value_type* __first,
  83. const value_type* __last, const _Compare& __comp,
  84. const allocator_type& __a = allocator_type())
  85. : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
  86. set(const_iterator __first, const_iterator __last)
  87. : _M_t(_Compare(), allocator_type())
  88. { _M_t.insert_unique(__first, __last); }
  89. set(const_iterator __first, const_iterator __last, const _Compare& __comp,
  90. const allocator_type& __a = allocator_type())
  91. : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
  92. #endif /* __STL_MEMBER_TEMPLATES */
  93. set(const set<_Key,_Compare,_Alloc>& __x) : _M_t(__x._M_t) {}
  94. set<_Key,_Compare,_Alloc>& operator=(const set<_Key, _Compare, _Alloc>& __x)
  95. {
  96. _M_t = __x._M_t;//调用了底层红黑树的operator=操作函数
  97. return *this;
  98. }
  99. //以下所有的set操作行为,RB-tree都已提供,所以set只要调用即可
  100. // accessors:
  101. //返回用于key比较的函数,调用RB-Tree的key_comp()
  102. key_compare key_comp() const { return _M_t.key_comp(); }
  103. //由于set的性质, value和key使用同一个比较函数
  104. value_compare value_comp() const { return _M_t.key_comp(); }
  105. //获取分配器类型
  106. allocator_type get_allocator() const { return _M_t.get_allocator(); }
  107. //以下是一些set的基本迭代器所指位置
  108. iterator begin() const { return _M_t.begin(); }
  109. iterator end() const { return _M_t.end(); }
  110. reverse_iterator rbegin() const { return _M_t.rbegin(); }
  111. reverse_iterator rend() const { return _M_t.rend(); }
  112. //以下的函数都是调用了RB-Tree的实现,set不必自己定义
  113. bool empty() const { return _M_t.empty(); }
  114. size_type size() const { return _M_t.size(); }
  115. size_type max_size() const { return _M_t.max_size(); }
  116. //这里调用的swap()函数是专属于RB-Tree的swap(),并不是STL的swap()算法
  117. void swap(set<_Key,_Compare,_Alloc>& __x) { _M_t.swap(__x._M_t); }
  118. // insert/erase
  119. // 根据返回值的情况,判断是否插入该元素
  120. //pair.second为true则表示已插入该元素
  121. //为false则表示set中已存在与待插入相同的元素, 不会重复插入
  122. pair<iterator,bool> insert(const value_type& __x) {
  123. pair<typename _Rep_type::iterator, bool> __p = _M_t.insert_unique(__x);
  124. return pair<iterator, bool>(__p.first, __p.second);
  125. }
  126. //在指定位置插入元素,但是会先遍历该集合,判断是否存在相同元素
  127. //若不存在才在指定位置插入该元素
  128. iterator insert(iterator __position, const value_type& __x) {
  129. typedef typename _Rep_type::iterator _Rep_iterator;
  130. return _M_t.insert_unique((_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_unique(__first, __last);
  136. }
  137. #else
  138. void insert(const_iterator __first, const_iterator __last) {
  139. _M_t.insert_unique(__first, __last);
  140. }
  141. void insert(const value_type* __first, const value_type* __last) {
  142. _M_t.insert_unique(__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. //清除set
  160. void clear() { _M_t.clear(); }
  161. // set 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 {
  166. return _M_t.find(__x) == _M_t.end() ? 0 : 1;
  167. }
  168. //返回不小于当前元素的第一个可插入的位置
  169. iterator lower_bound(const key_type& __x) const {
  170. return _M_t.lower_bound(__x);
  171. }
  172. // 返回大于当前元素的第一个可插入的位置
  173. iterator upper_bound(const key_type& __x) const {
  174. return _M_t.upper_bound(__x);
  175. }
  176. pair<iterator,iterator> equal_range(const key_type& __x) const {
  177. return _M_t.equal_range(__x);
  178. }
  179. #ifdef __STL_TEMPLATE_FRIENDS
  180. template <class _K1, class _C1, class _A1>
  181. friend bool operator== (const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&);
  182. template <class _K1, class _C1, class _A1>
  183. friend bool operator< (const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&);
  184. #else /* __STL_TEMPLATE_FRIENDS */
  185. friend bool __STD_QUALIFIER
  186. operator== __STL_NULL_TMPL_ARGS (const set&, const set&);
  187. friend bool __STD_QUALIFIER
  188. operator< __STL_NULL_TMPL_ARGS (const set&, const set&);
  189. #endif /* __STL_TEMPLATE_FRIENDS */};
  190. template <class _Key, class _Compare, class _Alloc>
  191. inline bool operator==(const set<_Key,_Compare,_Alloc>& __x,
  192. const set<_Key,_Compare,_Alloc>& __y) {
  193. return __x._M_t == __y._M_t;}
  194. template <class _Key, class _Compare, class _Alloc>
  195. inline bool operator<(const set<_Key,_Compare,_Alloc>& __x,
  196. const set<_Key,_Compare,_Alloc>& __y) {
  197. return __x._M_t < __y._M_t;}
  198. #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
  199. template <class _Key, class _Compare, class _Alloc>
  200. inline bool operator!=(const set<_Key,_Compare,_Alloc>& __x,
  201. const set<_Key,_Compare,_Alloc>& __y) {
  202. return !(__x == __y);}
  203. template <class _Key, class _Compare, class _Alloc>
  204. inline bool operator>(const set<_Key,_Compare,_Alloc>& __x,
  205. const set<_Key,_Compare,_Alloc>& __y) {
  206. return __y < __x;}
  207. template <class _Key, class _Compare, class _Alloc>
  208. inline bool operator<=(const set<_Key,_Compare,_Alloc>& __x,
  209. const set<_Key,_Compare,_Alloc>& __y) {
  210. return !(__y < __x);}
  211. template <class _Key, class _Compare, class _Alloc>
  212. inline bool operator>=(const set<_Key,_Compare,_Alloc>& __x,
  213. const set<_Key,_Compare,_Alloc>& __y) {
  214. return !(__x < __y);}
  215. template <class _Key, class _Compare, class _Alloc>
  216. inline void swap(set<_Key,_Compare,_Alloc>& __x,
  217. set<_Key,_Compare,_Alloc>& __y) {
  218. __x.swap(__y);}
  219. #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
  220. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  221. #pragma reset woff 1174
  222. #pragma reset woff 1375
  223. #endif
  224. __STL_END_NAMESPACE
  225. #endif /* __SGI_STL_INTERNAL_SET_H */
  226. // Local Variables:// mode:C++// End: