1. 程式人生 > >基於stl序列容器實現的通用集合類

基於stl序列容器實現的通用集合類

{
 23    typedef U<T> Allocator;
 24    typedef C<T,Allocator>base;
 25
 26public:
 27    //為使用方便,重新定義實現建構函式及拷貝建構函式,但賦值拷貝可以不用重新定義實現 28    STLCollection()
 29    {
 30    }
 31    explicit STLCollection(const Allocator& al)
 32        :base(al)
 33    {
 34    }
 35    explicit STLCollection(size_t n)
 36        :base(n)
 37    {
 38    }
 39    STLCollection(size_t n,const T& t)
 40        :base(n,t)
 41    {
 42    }
 43    STLCollection(size_t n,const T& t,const Allocator& al)
 44        :base(n,t,al)
 45    {
 46    }
 47    STLCollection(const STLCollection& right)
 48        :base(right)
 49    {
 50    }
 51
 52    template<class InputIterator> 53    STLCollection(InputIterator first,InputIterator last)
 54        :base(first,last)
 55    {
 56    }
 57
 58    template<class InputIterator> 59    STLCollection(InputIterator first,InputIterator last,const Allocator& al)
 60        :
base(first,last,al)
 61    {
 62    }
 63
 64public:
 65    //使基類的同名函式erase,insert,front,back可見 66usingbase::erase;
 67    usingbase::insert;
 68    usingbase::front;
 69    usingbase::back;
 70
 71    void add(const T& t,bool append =true)
 72    {
 73        if (append)
 74            base::insert(base::end(),t);
 75        else 76            base::insert(base::begin(),t);
 77    }
 78    void insert(size_t index,const T& t)
 79    {
 80        insert_impl(index,t,typename std::iterator_traits<typename base::iterator>::iterator_category());
 81    }
 82    void erase(size_t index)
 83    {
 84        erase_impl(index,typename std::iterator_traits<typename base::iterator>::iterator_category());
 85    }
 86    void erase(size_t beg,size_t end)
 87    {
 88        erase_impl(beg,end,typename std::iterator_traits<typename base::iterator>::iterator_category());
 89    }
 90    voidset(size_t index,const T& t)
 91    {
 92        T* p =get(index);
 93        if (p) *= t;
 94    }
 95    T*get(size_t index) 
 96    {
 97        return get_impl(index,typename std::iterator_traits<typename base::iterator>::iterator_category());
 98    }
 99    const T*get(size_t index) const100    {
101        return get_impl(index,typename std::iterator_traits<typename base::iterator>::iterator_category());
102    }
103    T* front()
104    {
105        if (base::empty()) return NULL;
106        return&base::front();
107    }
108    const T* front() const109    {
110        if (base::empty()) return NULL;
111        return&base::front();
112    }
113    T* back()
114    {
115        if (base::empty()) return NULL;
116        return&base::back();
117    }
118    const T* back() const119    {
120        if (base::empty()) return NULL;
121        return&base::back();
122    }
123    bool is_empty() const124    {
125        returnbase::empty();
126    }
127
128private:
129    /*************************************************************************************
130        下面函式僅作內部實現,需要注意以下幾點
131        (1) 不讓其子類和外部可見,故使用private訪問控制
132        (2) 考慮到子類可能會使用using指令來引用,如果為過載形式,子類using引用同名函式
133             會因為private出錯而不能引用public同名函式,故特命名為xxx_impl而非過載形式
134    *************************************************************************************/
135    void insert_impl(size_t index,const T& t,std::random_access_iterator_tag tag)
136    {
137        if (index <base::size())
138        {
139            base::insert(base::begin()+index,t);
140        }
141    }
142    void insert_impl(size_t index,const T& t,std::input_iterator_tag tag)
143