移动网站建设价格,西安专业网站建设服务,前端简历,学做古装网站仿照vector手动实现自己的myVector#xff0c;最主要实现二倍扩容功能
#include iostreamusing namespace std;
template typename T
class Myvector
{
private:T *start;//起始指针T *end;//数组末尾指针T *last;//数组有效长度的尾指针
public://定义无参构… 仿照vector手动实现自己的myVector最主要实现二倍扩容功能
#include iostreamusing namespace std;
template typename T
class Myvector
{
private:T *start;//起始指针T *end;//数组末尾指针T *last;//数组有效长度的尾指针
public://定义无参构造Myvector(){startnew T[2];laststart;endstart1;}//定义有参构造Myvector(int num,const T val){startnew T[num1];laststart;endstartnum;for(int i0;inum;i){start[i]val;last;}}//定义拷贝构造函数Myvector(const MyvectorT *other){this-startnew T[other-end -other-first1];this-lastother-last;this-endother-end;for(int i0;iother-end-other-start;i){this-first[i]other-first[i];}}//定义拷贝赋值函数Myvector operator(const MyvectorT*other){if(this!other){delete []start;this-firstnew T[other-end-other-start1];this-lastother-last;this-endother-end;for(int i0;iother-end-other-start;i){this-start[i]other-start[i];}}return *this;}//析构函数~Myvector(){delete []start;startnullptr;lastnullptr;endnullptr;}//at()函数T at(int pos){if(posend-start){cout越界了endl;}return start[pos];}//判空bool empty(){if(laststart){return 1;}else{return 0;}}//front()函数T front(){return *start;}//back()函数T back(){return *(end-1);}//size()函数int size(){return last-start;}//二倍扩容void erkr(){if(end-start1||laststart){int lenend-start;startnew T[len*2];}last(end-start)-1;return;}//push_back()void push_back(const T val){if(lastend)//容器满了{erkr();}*lastval;last;}//pop_back()void pop_back()//容器是空的{if(empty()){cout容器空了endl;}last--;}//begin()返回第一个元素的迭代器T*begin()const{return start;}//end()T*pend(){return last;}};
int main()
{Myvectorintm(2,5);coutm.at(1)endl;coutm.size()endl;//大小m.push_back(6);coutm.size()endl;//大小Myvectorintn(m);n.pop_back();coutm.at(1)endl;return 0;
}思维导图