仿写实现Myvector

#include <iostream>
#include<vector>
using namespace std;
template <typename T>
class Myvector
{
public:
    T* start;
    T* last;
    T* end;
public:
    Myvector():start(new T [10]),last(start),end(start+10)
    {}
    Myvector(size_t num , const T &val)
    {
        this->start = new T [num];
        this->last = this->start;
        this->end = this->last +num;
        for(auto i = 0 ; i < num; i++)
        {
            start[i] = val;
            this->last++;

        }


    }

    Myvector(const Myvector &other)
    {
        this->start = new T (other.end-other.start);
        this->last = this->start+(other.last - other.start);
        this->end = this->start +(other.end-other.start);
        int i = 0;
        for (T* p = this->start ; p <= this->end; p++)
        {
            p[0] = other.start[i];
            i++;
        }
    }

    Myvector &operator =(const T& other)
    {
        if(this != other)
        {
            this->start = new T (other.end-other.start);
            this->last = this->start+(other.last - other.start);
            this->end = this->start +(other.end-other.start);
            int i = 0;
            for (T* p = this->start ; p <= this->end; p++)
            {
                p[0] = other.start[i];
                i++;
            }
        }
        return  *this;
    }

    ~Myvector()
    {

        delete []start;
        start =nullptr;
        last =nullptr;
        end = nullptr;
    }

    T at(int i)
    {
        if(i<0||i>(this->last-this->start))      //i>(this->last-this->start)/sizeof (T)
        {
            throw string ("error");
        }
        else
        {
            return *(this->start+i);
        }
    }

    bool empty()
    {
        return this->last==this->start?true:false;
    }
    bool full()
    {
        return this->last==this->end?true:false;
    }

    T & front()
    {
        return *this->start;
    }

    T & back()
    {
        return *(this->last-1);
    }

    T size()
    {
        return this->last - this->start;
    }
    void clear()
    {
        this->last = this->start;
        this->end = this->start+1;
    }
    void expand()
    {
        T size = end-start;
        T * temp = new T (2*(end-start));
        memcpy(temp,start,sizeof (T)*(end-start));
        delete []start;
        start = temp;
        last = start +size;
        end = start +2*size;
    }

    void push_back(const T &data)
    {
        if(this->full())
        {
            this->expand();
        }

        *(this->last) = data;
        last++;

    }
    void pop_back()
    {
        if(this->empty())
        {throw string ("error");}

        else
        {
            last --;
        }
    }
};
int main()
{
    Myvector <int> v1;
    Myvector <int> v2 (1,5) ;
    Myvector <int> v3(v2) ;
    v3.push_back(9);
    try {
        cout<<v3.at(3)<<endl;
    } catch (string msg) { cout<<msg<<endl;

                         }
    cout<<"v3.back() = "<<v3.back()<<endl;
    cout <<"v3.size() = "<< v3.size() <<endl;
    v3.pop_back();
    cout<<"v3.front() ="<<v3.front()<<endl;
    cout<<"v3.back() ="<<v3.back()<<endl;


    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值