简单重写容器vector

  1 #pragma once
  2 #include <iostream>
  3 using namespace std;
  4 
  5 template<class T>
  6 class CMyVector
  7 {
  8 public:
  9     CMyVector() { buff = NULL; len = maxSize = 0; }
 10     ~CMyVector() { _clear(); }
 11     size_t capacity();
 12     size_t size();
 13     bool empty();
 14     void print();
 15     //迭代器
 16     struct MyIterator
 17     {
 18         T* pt;
 19         MyIterator() { pt = NULL; }
 20         MyIterator(MyIterator& it) { pt = it.pt; }
 21         ~MyIterator() { pt = NULL; }
 22         MyIterator operator=(const MyIterator& it) {
 23             pt = it.pt;
 24             return *this;
 25         }
 26         bool operator!=(const MyIterator& it) {
 27             return (pt != it.pt);
 28         }
 29         T operator*() {
 30             return (*pt);
 31         }
 32         MyIterator& operator++() {
 33             ++pt;
 34             return *this;
 35         }
 36         MyIterator operator++(int) {
 37             MyIterator tmp = *this;
 38             ++pt;
 39             return tmp;
 40         }
 41         MyIterator operator+(int n)const {
 42             MyIterator tmp;
 43             tmp.pt = pt + n;
 44             return tmp;
 45         }
 46         int operator-(const MyIterator& it)const {
 47             return (pt - it.pt);
 48         }
 49         MyIterator operator-(int n) {
 50             MyIterator tmp;
 51             tmp.pt = pt - n;
 52             return tmp;
 53         };
 54     };
 55 
 56     MyIterator begin()
 57     {
 58         MyIterator tmp;
 59         tmp.pt = buff + 0;
 60         return tmp;
 61     }
 62     MyIterator end()
 63     {
 64         MyIterator tmp;
 65         tmp.pt = buff + len;
 66         return tmp;
 67     }
 68 
 69     T operator[](size_t pos) {
 70         return *(buff + pos);
 71     }
 72 
 73     void push_back(T t);
 74     void insert(const MyIterator& it, T t);
 75     void insert(const MyIterator& it, const MyIterator& first, const MyIterator& last);
 76     void assign(const MyIterator& first, const MyIterator& last);
 77     T at(size_t pos);
 78 private:
 79     T* buff;
 80     size_t len;
 81     size_t maxSize;
 82 
 83     void _clear();
 84 };
 85 
 86 template<class T>
 87 void CMyVector<T>::push_back(T t)
 88 {
 89     if (len >= maxSize)
 90     {
 91         maxSize += ((maxSize >> 1) > 1) ? (maxSize >> 1) : 1;
 92         T* tmp = new T[maxSize];
 93         if (buff)
 94         {
 95             memcpy(tmp, buff, sizeof(T)*len);
 96             delete[] buff;
 97         }
 98         buff = tmp;
 99     }
100     buff[len++] = t;
101 }
102 
103 template<class T>
104 void CMyVector<T>::insert(const MyIterator& it, T t)
105 {
106     int pos = it - begin();
107     if (pos > (int)len)
108         return;
109 
110     if (len >= maxSize)
111         maxSize += ((maxSize >> 1) > 1) ? (maxSize >> 1) : 1;
112 
113 
114     T* tmp = new T[maxSize];
115     if (buff) {
116         memcpy(tmp, buff, sizeof(T)*pos);
117         tmp[pos] = t;
118         memcpy(tmp + pos + 1, buff + pos, sizeof(T)*(len - pos));
119         delete[] buff;
120     }
121     else
122         tmp[pos] = t;
123     buff = tmp;
124     ++len;
125 }
126 
127 template<class T>
128 void CMyVector<T>::insert(const MyIterator& it, const MyIterator& first, const MyIterator& last)
129 {
130     int pos = it - begin();
131     if (pos > (int)len)
132         return;
133 
134     int count = 0;
135     if ((count = last - first) <= 0)
136         return;
137 
138     while ((len + count) >= maxSize)
139         maxSize += ((maxSize >> 1) > 1) ? (maxSize >> 1) : 1;
140 
141 
142     T* tmp = new T[maxSize];
143     if (buff) {
144         memcpy(tmp, buff, sizeof(T)*pos);
145         for (int i = 0; i < count; ++i)
146             tmp[pos + i] = *(first + i);
147 
148         memcpy(tmp + pos + count, buff + pos, sizeof(T)*(len - pos));
149         delete[] buff;
150     }
151     else
152         for (int i = 0; i < count; ++i)
153             tmp[pos + i] = *(first + i);
154 
155     buff = tmp;
156     len += count;
157 
158 }
159 
160 template<class T>
161 void CMyVector<T>::assign(const MyIterator& first, const MyIterator& last)
162 {
163     _clear();
164     insert(begin(), first, last);
165 }
166 
167 template<class T>
168 T CMyVector<T>::at(size_t pos)
169 {
170     return *(buff + pos);
171 }
172 
173 template<class T>
174 void CMyVector<T>::_clear()
175 {
176     if (buff)
177         delete[] buff;
178 
179     buff = NULL;
180     len = maxSize = 0;
181 }
182 
183 template<class T>
184 size_t CMyVector<T>::capacity()
185 {
186     return maxSize;
187 }
188 
189 template<class T>
190 size_t CMyVector<T>::size()
191 {
192     return len;
193 }
194 
195 template<class T>
196 bool CMyVector<T>::empty()
197 {
198     return (len == 0);
199 }
200 
201 template<class T>
202 void CMyVector<T>::print()
203 {
204     /*CMyVector<T>::MyIterator it;
205     it = begin();
206     while(it != end())
207     cout << *it++ << " ";*/
208     for (auto it = begin(); it != end(); ++it)
209     {
210         cout << *it << " ";
211     }
212     cout << endl;
213 }
原文地址:https://www.cnblogs.com/xslwm/p/7966563.html