STL源码 SET 解析

这个是阿里春招实习二面的时候面试官问的一个问题。

SET是STL中的标准容器,SET里面的元素会根据键值自动排序,它不像map那样拥有实值value和键值key的对应,set只有实值。SET的底层实现时RB-tree,当插入到RB-tree中后,其值不能再更改,因为更改就意味着可能不符合RB-tree的特性了,所以其迭代器set<T>::iterator是RB-tree的constrant iterator。由于SET底层是RB-tree,所以SET在插入等操作之后,迭代器不会失效,但删除元素的迭代器是个例外。


[cpp]  view plain  copy
  1. G++ 2.91.57,cygnus\cygwin-b20\include\g++\stl_set.h 完整列表  
  2. /* 
  3.  * 
  4.  * Copyright (c) 1994 
  5.  * Hewlett-Packard Company 
  6.  * 
  7.  * Permission to use, copy, modify, distribute and sell this software 
  8.  * and its documentation for any purpose is hereby granted without fee, 
  9.  * provided that the above copyright notice appear in all copies and 
  10.  * that both that copyright notice and this permission notice appear 
  11.  * in supporting documentation.  Hewlett-Packard Company makes no 
  12.  * representations about the suitability of this software for any 
  13.  * purpose.  It is provided "as is" without express or implied warranty. 
  14.  * 
  15.  * 
  16.  * Copyright (c) 1996,1997 
  17.  * Silicon Graphics Computer Systems, Inc. 
  18.  * 
  19.  * Permission to use, copy, modify, distribute and sell this software 
  20.  * and its documentation for any purpose is hereby granted without fee, 
  21.  * provided that the above copyright notice appear in all copies and 
  22.  * that both that copyright notice and this permission notice appear 
  23.  * in supporting documentation.  Silicon Graphics makes no 
  24.  * representations about the suitability of this software for any 
  25.  * purpose.  It is provided "as is" without express or implied warranty. 
  26.  */  
  27.   
  28. /* NOTE: This is an internal header file, included by other STL headers. 
  29.  *   You should not attempt to use it directly. 
  30.  */  
  31.   
  32. #ifndef __SGI_STL_INTERNAL_SET_H  
  33. #define __SGI_STL_INTERNAL_SET_H  
  34.   
  35. __STL_BEGIN_NAMESPACE  
  36.   
  37. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)  
  38. #pragma set woff 1174  
  39. #endif  
  40. //less<Key>说明默认使用递增排序  
  41. #ifndef __STL_LIMITED_DEFAULT_TEMPLATES  
  42. template <class Key, class Compare = less<Key>, class Alloc = alloc>  
  43. #else  
  44. template <class Key, class Compare, class Alloc = alloc>  
  45. #endif  
  46. class set {  
  47. public:  
  48.   // typedefs:  
  49.   
  50.   //key_type和 value_type类型都是实值  
  51.   typedef Key key_type;  
  52.   typedef Key value_type;  
  53.   // 注意,以下 key_compare 和 value_compare 使用相同的比较函数  
  54.   typedef Compare key_compare;  
  55.   typedef Compare value_compare;  
  56. private:  
  57.   /* 注意,identity 定义于 <stl_function.h>,参考第7章,其定义为: 
  58.     template <class T> 
  59.     struct identity : public unary_function<T, T> { 
  60.       const T& operator()(const T& x) const { return x; } 
  61.     }; 
  62.   */  
  63.   // 以下,rb_tree<Key, Value, KeyOfValue, Compare, Alloc>  
  64.   typedef rb_tree<key_type, value_type,   
  65.                      identity<value_type>, key_compare, Alloc> rep_type;  
  66.   rep_type t;  // 底层采用红黑树  
  67. public:  
  68.   typedef typename rep_type::const_pointer pointer;  
  69.   typedef typename rep_type::const_pointer const_pointer;  
  70.   typedef typename rep_type::const_reference reference;  
  71.   typedef typename rep_type::const_reference const_reference;  
  72.   //用的是RB-tree的 const_iterator。不允许修改其值,也不允许使用者在任意处插入元素  
  73.   typedef typename rep_type::const_iterator iterator;  
  74.     
  75.   typedef typename rep_type::const_iterator const_iterator;  
  76.   typedef typename rep_type::const_reverse_iterator reverse_iterator;  
  77.   typedef typename rep_type::const_reverse_iterator const_reverse_iterator;  
  78.   typedef typename rep_type::size_type size_type;  
  79.   typedef typename rep_type::difference_type difference_type;  
  80.   
  81.   // allocation/deallocation  
  82.   // 注意, set 一定使用 insert_unique() 而不使用 insert_equal()。  
  83.   // multiset 才使用 insert_equal()。  
  84.   set() : t(Compare()) {}  
  85.   explicit set(const Compare& comp) : t(comp) {}  
  86. //初始化SET  
  87. #ifdef __STL_MEMBER_TEMPLATES  
  88.   template <class InputIterator>  
  89.   set(InputIterator first, InputIterator last)  
  90.     : t(Compare()) { t.insert_unique(first, last); }  
  91.   
  92.   template <class InputIterator>  
  93.   set(InputIterator first, InputIterator last, const Compare& comp)  
  94.     : t(comp) { t.insert_unique(first, last); }  
  95. #else  
  96.   set(const value_type* first, const value_type* last)   
  97.     : t(Compare()) { t.insert_unique(first, last); }  
  98.   set(const value_type* first, const value_type* last, const Compare& comp)  
  99.     : t(comp) { t.insert_unique(first, last); }  
  100.   
  101.   set(const_iterator first, const_iterator last)  
  102.     : t(Compare()) { t.insert_unique(first, last); }  
  103.   set(const_iterator first, const_iterator last, const Compare& comp)  
  104.     : t(comp) { t.insert_unique(first, last); }  
  105. #endif /* __STL_MEMBER_TEMPLATES */  
  106.   
  107.   set(const set<Key, Compare, Alloc>& x) : t(x.t) {}  
  108.   set<Key, Compare, Alloc>& operator=(const set<Key, Compare, Alloc>& x) {   
  109.     t = x.t;   
  110.     return *this;  
  111.   }  
  112.   
  113.   //一下所有的SET操作,RB-tree都已经提供,SET只是调用而已  
  114.     
  115.   // accessors:  
  116.   key_compare key_comp() const { return t.key_comp(); }  
  117.   // 以下注意,set 的value_comp() 实际上就是RB-tree 的key_comp()。  
  118.   value_compare value_comp() const { return t.key_comp(); }  
  119.   iterator begin() const { return t.begin(); }  
  120.   iterator end() const { return t.end(); }  
  121.   reverse_iterator rbegin() const { return t.rbegin(); }   
  122.   reverse_iterator rend() const { return t.rend(); }  
  123.   bool empty() const { return t.empty(); }  
  124.   size_type size() const { return t.size(); }  
  125.   size_type max_size() const { return t.max_size(); }  
  126.   void swap(set<Key, Compare, Alloc>& x) { t.swap(x.t); }  
  127.   
  128.   // insert/erase  
  129.   typedef  pair<iterator, bool> pair_iterator_bool;   
  130.   pair<iterator,bool> insert(const value_type& x) {   
  131.     pair<typename rep_type::iterator, bool> p = t.insert_unique(x);   
  132.     return pair<iterator, bool>(p.first, p.second);  
  133.   }  
  134.   iterator insert(iterator position, const value_type& x) {  
  135.     typedef typename rep_type::iterator rep_iterator;  
  136.     return t.insert_unique((rep_iterator&)position, x);  
  137.   }  
  138. #ifdef __STL_MEMBER_TEMPLATES  
  139.   template <class InputIterator>  
  140.   void insert(InputIterator first, InputIterator last) {  
  141.     t.insert_unique(first, last);  
  142.   }  
  143. #else  
  144.   void insert(const_iterator first, const_iterator last) {  
  145.     t.insert_unique(first, last);  
  146.   }  
  147.   void insert(const value_type* first, const value_type* last) {  
  148.     t.insert_unique(first, last);  
  149.   }  
  150. #endif /* __STL_MEMBER_TEMPLATES */  
  151.   void erase(iterator position) {   
  152.     typedef typename rep_type::iterator rep_iterator;  
  153.     t.erase((rep_iterator&)position);   
  154.   }  
  155.   size_type erase(const key_type& x) {   
  156.     return t.erase(x);   
  157.   }  
  158.   void erase(iterator first, iterator last) {   
  159.     typedef typename rep_type::iterator rep_iterator;  
  160.     t.erase((rep_iterator&)first, (rep_iterator&)last);   
  161.   }  
  162.   void clear() { t.clear(); }  
  163.   
  164.   // set operations:  
  165.   //使用的是RB-tree的搜索函数,而不是STL的find,STL的find的只是  
  166.   //循序搜索,效率不如关联容器自己定义的效率高  
  167.   iterator find(const key_type& x) const { return t.find(x); }  
  168.   size_type count(const key_type& x) const { return t.count(x); }  
  169.   iterator lower_bound(const key_type& x) const {  
  170.     return t.lower_bound(x);  
  171.   }  
  172.   iterator upper_bound(const key_type& x) const {  
  173.     return t.upper_bound(x);   
  174.   }  
  175.   pair<iterator,iterator> equal_range(const key_type& x) const {  
  176.     return t.equal_range(x);  
  177.   }  
  178.   friend bool operator== __STL_NULL_TMPL_ARGS (const set&, const set&);  
  179.   friend bool operator< __STL_NULL_TMPL_ARGS (const set&, const set&);  
  180. };  
  181.   
  182. template <class Key, class Compare, class Alloc>  
  183. inline bool operator==(const set<Key, Compare, Alloc>& x,   
  184.                        const set<Key, Compare, Alloc>& y) {  
  185.   return x.t == y.t;  
  186. }  
  187.   
  188. template <class Key, class Compare, class Alloc>  
  189. inline bool operator<(const set<Key, Compare, Alloc>& x,   
  190.                       const set<Key, Compare, Alloc>& y) {  
  191.   return x.t < y.t;  
  192. }  
  193.   
  194. #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER  
  195.   
  196. template <class Key, class Compare, class Alloc>  
  197. inline void swap(set<Key, Compare, Alloc>& x,   
  198.                  set<Key, Compare, Alloc>& y) {  
  199.   x.swap(y);  
  200. }  
  201.   
  202. #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */  
  203.   
  204. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)  
  205. #pragma reset woff 1174  
  206. #endif  
  207.   
  208. __STL_END_NAMESPACE  
  209.   
  210. #endif /* __SGI_STL_INTERNAL_SET_H */  
  211.   
  212. // Local Variables:  
  213. // mode:C++  
  214. // End:  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值