vector内部实现2

push_back 往动态数组的内部进行添加数据

pop_back 往动态数组的尾部进行删除数据

resize  讲元素的数量len改成num个数量  如果size()变大了,多出来的将用默认构造来创建

assert(nullptr);断言函数 可以解决出错问题 和return,throw相似 进行处理异常数据

reserve(int n);如果容器的容量不足扩大容量

maxSize=maxSize+(maxSize>>1)>1?(maxSize>>1):1)  内存不够会发生内存重分配 把原来的内存数据拷贝到新的内存中去

1 2 3 4 6 9 13 19

vector<int>::iterator vit;容器的迭代器

迭代器 是用来遍历容器中部分或者全部容器的对象

结构和类的区分 访问权限不是主要的区分方式 对象中有接口用类 对象里面没有接口就用结构 迭代器用结构来模拟

.h文件不参与编译 只是一个工具 

end和begin是指向vertor里面容器的开始和结尾 insert和erase是插入和删除

自己模拟的动态数组在元素删除时不会有内存重分配,但是系统vector在删除时候回导致内存重分配

具体内容由下面实现

  1 #pragma once
  2 template<typename T>
  3 class Myvector
  4 {
  5     T *pBuff;//给一个指针用来指向以恶个动态内存
  6     unsigned int len;//给一个长度 表示这个内存中的元素个数
  7     size_t maxSize;//存放这个动态数组的内存空间的大小
  8 public:
  9     struct MyIterator//这个结构也是一个模板 一个模板内部嵌套定义了一个末班
 10     {
 11         T *pIt;
 12         MyIterator & operator = (MyIterator const& srcIt)
 13         {
 14             pIt=srcIt.pIt;
 15             return *this;
 16         }
 17         bool operator!=(MyIterator const& srcIt) const
 18         {
 19             return pIt!=srcIt.pIt;        
 20         }
 21         MyIterator & operator ++ ()//前置
 22         {
 23             pIt++;
 24             return *this;
 25         }    
 26         MyIterator  operator++ (int)//后置++
 27         {
 28             MyIterator tempIt=*this;
 29             pIt++;
 30             return tempIt;
 31         }
 32         T &operator*()
 33         {
 34             return *pIt;
 35         }
 36         T &operator-(MyIterator const& scrIt)const
 37         {
 38             return pIt-scrIt.pIt;
 39         }
 40         MyIterator operator +(int n)
 41         {
 42             MyIterator tempIt = *this;
 43             tempIt.pIt+=n;
 44             return tempIt;
 45         }
 46     };
 47 public:
 48     MyIterator begin()//得到容器的第一个位置
 49     {
 50         MyIterator tempIt;
 51         tempIt.pIt=pBuff;
 52         return templt;
 53     }
 54     MyIterator end()//得到容器的最后一个元素的下一个位置
 55     {
 56         MyIterator tempIt;
 57         tempIt.pIt=pBuff+len;
 58         return tempIt;
 59     }
 60 public:
 61     MyIterator insert(MyIterator const& pos,MyIterator const& firstMyIterator const& second)//插入,在pos处插入n个elem元素
 62     {
 63         MyIterator tempIt=pos;
 64         int n=second-first;
 65                 for(int i=o;i<n;i++)
 66             tempIt=insett(templt,*(first+i));//在insert里面调用重载的另一个insert
 67         return tempIt;
 68     }
 69     MyIterator insert(MyIterator const& pos,int n,T const& elem)//插入,在pos处插入n个elem元素
 70     {
 71         MyIterator tempIt=pos;
 72         for(int i=o;i<n;i++)
 73             tempIt=insett(templt,elem);//在insert里面调用重载的另一个insert
 74         return tempIt;
 75     }
 76     MyIterator insert(MyIterator const& pos,T const& elem)//插入,在pos这个股迭代器所指的位置插入一个elem元素
 77     {
 78         int index=pos.pIt-pBuff;//通过迭代器位置和容器的首地址进行相减 得到当前的下标
 79         if(len>=maxSize)
 80         {
 81         maxSize=maxSize+(maxSize>>1)>1?(maxSize>>1):1);
 82         T *tempBuff=new T[maxSize];
 83         for(size_t i=0;i<len;++i)
 84             tempBuff[i]=pBuff[i];
 85         if(pBuff!=nullptr)
 86             delete[] pBuff;
 87         pBuff=tempBuff;
 88         }
 89         //移位操作
 90         for(size_t i=len;i>index;--i)
 91         {
 92             pBuff[i]=pBuff[i-1];
 93         }
 94         pBuff[index]=elem;
 95         len++;
 96         Myterator tempIt;
 97         tempIt.pIt=pBuff+index;
 98         return tempIt;
 99     }
100     //注意:自己模拟的动态数组在元素删除时不会有内存重分配,但是系统vector在删除时候回导致内存重分配
101     MyIterator erase(MyIterator const& pos)//删除代码
102     {
103         int index=pos.pIt-pBuff;
104         for(sieze_t i=index;i<len-1;i++)
105         {
106             pBuff[i]=pBuff[i+1];
107         }
108         len--;
109         Myterator tempIt;
110         tempIt.pIt=pBuff+index;
111         return tempIt;
112     }
113     MyIterator erase(MyIterator const& firstMyIterator const& second)
114     {
115         int n=second-first;
116         MyIterator tempIt=first;
117         for(int i=0;i<n;++i)
118         {
119             tempIt=erase(tempIt);
120             return tempIt;
121         }
122     }
123 public:
124     Myvector();//默认构造
125     Myvector(int n);//有n个T的默认构造对象 构造进这个容器
126     Myvector(int n,T const& elem);//用n个elem对象 构造进这个容器
127     Myvector(Myvector const& other);//深拷贝
128     //Myvector();
129 
130     ~Myvector();//析构函数 释放所有的对象 也会将变量删除(堆内存和栈内存)
131     void clear();//清除 (删除堆内存里数据)
132 public:
133     size_t size() const;
134     size_t capacit() const;//返回容器代下
135     bool empty() const;//判断容器是否为空
136 public:
137     bool operator==(Myvector const& srcVector) const;//运算符重载==
138     bool operator!=(Myvector const& srcVector) const; //重载运算符!=
139 public:
140     void assgin(int n, T const& elem);//赋值
141     void swap(Myvector & srcVerctor);//交换
142 public:
143     T at(int index);//返回动态数组下标为index的元素 是c++中唯一会主动抛异常的函数
144     T operator[](int index);//重载[]运算符
145     T front();//得到容器中的第一个元素,不管容器为不为空
146     T back();
147 public:
148     void push_back(T const& elem);//往动态数组的 尾部添加数字
149     void pop_back(); //往动态数组的尾部进行删除数据
150 public:
151     void resize(int num);//讲元素的数量len改成num个数量  如果size()变大了,多出来的将用默认构造来创建
152     void resize(int num,const& elem);//resize的重载
153     void reserve(int num);//如果容器的容量不足扩大容量
154 };
155 
156 template<typename T>
157 void Myvector<T>::Myvector()//无参构造
158 {
159     pBuff = nullptr;
160     len = 0;
161     maxSize = 0;
162 }
163 
164 template<typename T>
165 void Myvector<T>::~Myvector()//析构函数 清除对象
166 {
167     clear();//调用clear函数 重复代码通过封装函数来提高代码的效率
168 }
169 template<typename T>
170 void Myvector<T>::clear()
171 {
172     if (pBuff != nullptr)
173     {
174         delete[] pBuff;
175         pBuff = nullptr;
176         len = 0;
177         maxSize = 0;
178     }
179 }
180 template<typename T>
181 void Myvector<T>::Myvector(int n)//n个有T的默认构造的构造对象 构造进这个容器
182 {//n个对象构造这个容器
183     if (n < 0)
184     {
185         pBuff = nullptr;
186         len = 0;
187         maxSize = 0;
188     }
189     else
190     {
191         len = maxSize = n;
192         pBuff = new T[maxSize];
193     }
194 }
195 
196 template<typename T>
197 void Myvector<T>::Myvector(int n, T const& elem)//用n个elem对象来构造这个容器  &来增加传递效率
198 {
199     if (n < 0)
200     {
201         pBuff = nullptr;
202         len = 0;
203         maxSize = 0;
204     }
205     else
206     {
207         len = maxSize = n;
208         pBuff[i] = new T[maxSize];
209         for (size_t i = 0; i < len; ++i)//size_t无符号的int
210         {
211             pBuff[i] = elem;
212         }
213     }
214 }
215 template<typename T>
216 void Myvector<T>::Myvector(Myvector const& other)
217 {
218     len = other.len;
219     maxSize = other.maxSize;
220     pBuff = nullptr;
221     if (len > 0)
222     {
223         pBuff = new T[maxSize];
224         for (int i = 0; i < len; ++i)
225             pBuff[i] = other.pBuff[i];
226     }
227 }
228 template<typename T>
229 size_t Myvector<T>::size() const//返回容器的长度
230 {
231     return len;
232 }
233 template<typename T>
234 size_t Myvector<T>::capacity() const//返回容器大小
235 {
236     return maxSize;
237 }
238 template<typename T>
239 bool Myvector<T>::empty() const//判断容器是否为空
240 {
241     //return pBuff == nullptr;泵使用这一种 这一种判定存不存在
242     return len == 0;//是否为0 0是空的
243 }
244 template<typename T>
245 bool Myvector<T>::operator==(Myvector const& srcVector) const//判断容器是不是相等
246 {
247     if (len != srcVector.len)//长度不等
248         return false;
249     for (size_t i = 0; i < len; ++i)//遍历判定内容是否相等
250     {
251         if (pBuff[i] != srcVector.pBuff[i])
252             return false;
253     }
254     return true;
255 }
256 template<typename T>
257 bool Myvector<T>::operator==(Myvector const& srcVector) const//判断容器是不是相等
258 {
259     return !(*this == srcVector);
260 }
261 template<typename T>
262 void Myvector<T>::assign(int n, T const& elem)//和带参构造类似 对容器进行赋值
263 {
264     clear();//重点:清除自身可能原有的动态内存
265     if (n > 0)
266     {
267         len = maxSize = n;
268         pBuff = new T[maxSize];
269         for (size_t i = 0; i < len; ++i)
270             pBuff[i] = elem;
271     }
272 }
273 template<typename T>
274 void Myvector<T>::swap(Myvector & srcVector)
275 {
276     T *tempBuff = pBuff;
277     size_t tempLen = len;
278     size_t tempMaxSize = maxSize;//定义三个变量
279 
280     pBuff = srcVector.pBuff;
281     len = srcVector.len;
282     maxSize = srcVector.maxSize;//数据的交换
283 
284     srcVector.pBuff = tempBuff;
285     srcVector.len = tempLen;
286     srcVector.maxSize = tempMaxSize;
287 }
288 template<typename T>
289 T CMyVector<T>::at(int index)//唯一的会主动抛异常的函数
290 {
291     if (index < 0 || index >= len)
292         throw "out_of_range";//防止越界 抛异常
293     return pBuff[index];
294 }
295 
296 template<typename T>
297 T CMyVector<T>::operator[](int index)//这个应该出错就出错 无法出来 按照给的要求来
298 {
299     return pBuff[index];
300 }
301 template<typename T>
302 T Myvector<T>::back()
303 {
304     return pBuff[len - 1];
305 }
306 
307 template<typename T>
308 T Myvector<T>::front()
309 {
310     return pBuff[0];
311 }
312 
313 template<typename T>
314 T Myvector<T>::operator[](int index)
315 {
316     return pBuff[index];
317 }
318 template<typename T>
319 void Myvector<T>::push_back(T const& elem)
320 {
321     //1 2 3 4 6 9 13 19
322     if (len >= maxSize)
323     {
324         maxSize = maxSize + ((maxSize >> 1) > 1 ? (maxSize >> 1) : 1);
325         T *tempBuff = new T[maxSize];
326         for (size_t i = 0; i < len; ++i)
327             tempBuff[i] = pBuff[i];
328         if (pBuff != nullptr)
329             delete[] pBuff;
330         pBuff = tempBuff;
331     }
332     pBuff[len++] = elem;
333 }
334 template<typename T>
335 void Myvector<T>::pop_back()
336 {
337     --len;
338 }
339 template<typename T>
340 void Myvector<T>::resize(int num)
341 {
342     if(num<0)
343     {
344         //return;throw
345         assert(nullptr);//断言函数
346     }
347     if(num>len)//num超出扩张空间的时候 通过扩张len的长度 来进行对num的保存
348     {
349         while(num>=maxSize)
350             maxSize=maxSize+(maxSize>>1)>1?(maxSize>>1):1);
351         T *tempBuff=new T[maxSize];
352         for(size_t i=0;i<len;++i)
353             tempBuff[i]=pBuff[i];
354         if(pBuff!=nullptr)
355             delete[] pBuff;
356         pBuff=tempBuff;
357     }
358     len=num;
359 }
360 template<typename T>
361 void Myvector<T>::resize(int num,T const& elem)
362 {
363     if(num<0)
364     {
365         //return;throw
366         assert(nullptr);//断言函数
367     }
368     if(num>len)//num超出扩张空间的时候 通过扩张len的长度 来进行对num的保存
369     {
370         while(num>=maxSize)
371             maxSize=maxSize+(maxSize>>1)>1?(maxSize>>1):1);//
372         T *tempBuff=new T[maxSize];
373         for(size_t i=0;i<len;++i)
374             tempBuff[i]=pBuff[i];
375         if(pBuff!=nullptr)
376             delete[] pBuff;
377         pBuff=tempBuff;
378         for(size_t i=len;i<num;++i)
379             pBuff[i]=elem;
380     }
381     len=num;
382 }
383 template<typename T>
384 void Myvector<T>::reserve(int num)
385 {
386     if(num>maxSize)
387     {
388         maxSize=num;
389         T* tempBuff=new T[maxSize];
390         for(size_t i=0;i<len;++i)
391             tempBuff[i]=pBuff[i];
392         if(pBuff!=nullptr)
393             delete[] pBuff;
394         pBuff=tempBuff;
395     }

主函数实现头文件中一些函数的算法

 1 #include<vector>
 2 using std::vector;
 3 //写一个动态内存
 4 
 5 class CA{
 6     int a;
 7 public:
 8     CA(int i){ i = a; }
 9     CA();
10 };
11 template <class T>
12 class CStack
13 {
14     T pBuff[10];
15 public:
16     CStack();
17     ~CStack();
18     void push(T const& val);//入栈
19     void pop();//出栈
20     T GetTop();//得到栈顶的元素
21 };
22 template <class T>
23 void CStack<T>::pop()
24 {
25     top--;
26 }
27 
28 template <class T>
29 CStack<T>::CStack()
30 {
31     top = 0;
32 }
33 
34 template <class T>
35 void CStack<T>::push(T const& val)
36 {
37     pBuff[top++] = val;
38 }
39 int main()
40 {
41     CA a(1);//调用带参构造
42     CA pa[10];//调用默认构造  数组不可以用带参构造来构造
43     CA *pa1 = new CA;//调用默认构造
44 
45     vector<int> v;
46     vector<int>::iterator vit;//迭代器
47     for(vit=v.begin();vit!=v.end();++vit)
48     {
49         printf("%d
",*vit);
50     }
51 
52 
53     return 0;
54 }
原文地址:https://www.cnblogs.com/liugangjiayou/p/11417517.html