STL之关联式容器set

set概述
set的特性是,所有的元素都会根据元素的键值自动被排序。set的元素不像map那样可以同时拥有实值和键值,set元素的键值就是实值,实值就是键值。set不允许两个元素有相同的键值。
我们不可以通过set的迭代器改变元素的值,因为set元素值就是其键值,关系到set元素的排列规则。如果任意改变set元素值,会严重破坏set组织。稍后在源码中会看到set::iterator被定义为底层RB-tree的const_iterator,杜绝写入操作。set是一种constant iterator。
set拥有与list相同的某些性质:当客户端对它进行元素新增操作(insert)或删除操作(erase)时,操作之前的所有的迭代器,在操作完成之后都依然有效。当然,被删除的那个元素的迭代器必然是个意外。
STL特别提供了一组set/multset相关算法,包括交集set_intersection、联集 set_union、差集set_difference、对称差集set_symmetric_difference。
由于RB-tree是一种平衡二叉搜索树,自动排序的效果很不错,所以标准的STL set即以RB-tree为底层机制。由于set所开放的各种操作接口,RB-tree也提供了,所以几乎所有的set操作行为,都只是调用RB-tree的操作行为而已。
set源码摘录

template<class Key,class Compare=less<Key>>
class set
{
public:
         //typedefs:
         typedef Key key_type;
         typedef Key value_type;
         typedef Compare key_compare;
         typedef Compare value_compare;
privite:
         /*
              template<classs T>
               struct identity: public unary_function<T,T>
               {
                    const T& operator()(const T& x)const{const x;}
               };
         */
         typedef rb_tree<key_type,value_type,identity<value_type>,key_compare,Alloc>rep_type;
         rep_type t;
public:
         typedef typenemw rep_type::const_pointer pointer;
         typedef typenemw rep_type::const_pointer const_pointer;
         typedef typenemw rep_type::const_reference reference;
         typedef typenemw rep_type::const_reference const_reference;
         typedef typenemw rep_type::const_iterator iterator;
         typedef typenemw rep_type::const_iterator const_iterator;
         typedef typenemw rep_type::const_reverse_iterator reverse_iterator;
         typedef typenemw rep_type::const_reverse_iterator const_reverse_iterator;
         typedef typenemw rep_type::size_type size_type;
         typedef typenemw rep_type::difference_type difference_type;

        //allocation/deallocation
        //注意,set一定使用RB-tree的insert_unique()而非insert_equal()
        //mulyiset才使用RB-tree的insert_equal()
        //因为set不允许相同键值存在,multiset才允许相同键值存在
        set(): t(Compare()) {}
        explicit set(const Compare& comp): t(comp) {}
        
        template<class InputIterator>
        set(InputIterator first, InputIterator last): t(Compare()){t.insert_unique(first, last)}

        template<clsss InputIterator>
         set(InputIterator first, InputIterator last,const Compare& comp): t(comp){t.insert_unique(first, last)}
        
        set(const set<Key, Compare,Alloc>& x):t(x.t){}
        set<Key, Compare,Allloc>& operator=(const set<Key, Compare,Alloc>& x){return *this;}
 
        //以下所有的set操作行为,RB-tree都已提供,所以set只需要传递调用即可
        //accessors
        key_compare key_comp() const{return t.key_comp();}
        //以下注意,set的value_comp()事实上为RB-tree的key_comp()
        value_compare value_comp() const {return t.key_comp();}
        iterator begin() const {return t.begin();}
        iterator end() const {return t.end();}
        reverse_iterator rbegin() const {return t.rbegin();}
        reverse_iterator rend() const{return t.rend();}
        bool empty() const {return t.empty();}
        szie_type size()const{return t.size();}
        szie_type max_size()const {return t.max_size();}
        void swap(set<Key, Compare,Alloc>& x){t.swap(x.t);}

        //insert/erase
        typedef pair<iterator, bool>pair_iterator_bool
        pair<iterator, bool>insert(const value_type& x)
        {
             pair<typename rep_type::iterator,bool> p=t.insert_unique(x);
             return pair<iterator, bool>(p.first,p.second);
        }
        iterator insert(iterator position, const value_type& x)
        {
              typedef typename rep_type::iterator rep_iterator;
              return t.insert_unique((rep_iterator&)position, x);
        }
        template<calsss InputIterator>
        void insert(InputIterator first,InputIterator last)
        { 
              t.insert_unique(first,last);
        }
        void erase(iterator position)
        {
              typedef typename rep_type::iterator reo_iterator;
              t.erase((rep_iterator&)position);
        }
        size_type erase(const key_type& x)
        {
              return t.erase(x);
        }
        void erase(iterator first, iterator last)
        {
               typedef typename rep_type::iterator rep_iterator;
               t.erase((rep_iterator&)first, (rep_iteratot&)last);
        }
        void clear(){t.clear();}
        //set operations:
        iterator find(const key_type& x)const {return t.find();}
        size_type count (const key_type& x) const {return t.count(x);}
        iterator lower_bound(const key_type& x) const{return t.lower_bound(x);}
        iterator upper_bound(const key_type& x) const {return t.equal_range(x);}
        pair<iterator,iterator>equal_ranfe(const key_type& x) const{return t.equal_range(x);}
        friend bool operator==_STL_NULL_TMPL_ARGS(const set&,const set&);
        firend bool operator<_STL_NULL_TMPL_ARGS(const set&,const set&);
  };
  template<calss Key,class Compare, calss Alloc>
  {
         return x.t==y.t;
  }
  template <calss Key, class Compare, class Alloc>
  inline bool operator<(const set<key,Compare,Alloc>& x,const set<key,Compare,Alloc>&y){return x.t<y.t;}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值