c++day7

 仿照vector手动实现自己的myVector,最主要实现二倍扩容功能

#include 

using namespace std;
template 
class Myvector
{
private:
    T *start;//起始指针
    T *end;//数组末尾指针
    T *last;//数组有效长度的尾指针
public:
    //定义无参构造
    Myvector(){
        start=new T[2];
        last=start;
        end=start+1;
    }
    //定义有参构造
    Myvector(int num,const T &val)
    {
        start=new T[num+1];
        last=start;
        end=start+num;
        for(int i=0;i *other)
    {
        this->start=new T[other->end -other->first+1];
        this->last=other->last;
        this->end=other->end;
        for(int i=0;iend-other->start;++i)
        {
            this->first[i]=other->first[i];
        }
    }
    //定义拷贝赋值函数
    Myvector &operator=(const Myvector*other){
        if(this!=other)
        {
            delete []start;
            this->first=new T[other->end-other->start+1];
            this->last=other->last;
            this->end=other->end;
            for(int i=0;iend-other->start;i++)
            {
                this->start[i]=other->start[i];
            }
        }
        return  *this;
    }
    //析构函数
    ~Myvector()
    {
        delete []start;
        start=nullptr;
        last=nullptr;
        end=nullptr;
    }
    //at()函数
    T &at(int pos){
        if(pos>end-start)
        {
            cout<<"越界了"<m(2,5);
    cout<n(m);
    n.pop_back();
    cout<

c++day7_第1张图片

思维导图

c++day7_第2张图片

你可能感兴趣的:(c++,java,开发语言)