0401-0403作业

#include <iostream>

using namespace std;

template <typename T>
class my_vct
{
private:
    T*first;
    T*last;
    T*end;
public:
    my_vct(){}
    my_vct(int size)
    {
        first=new T[size];
        last=first;
        end=first+size;
    }
    ~my_vct()
    {
        delete []first;
        first=nullptr;
        last=nullptr;
        end=nullptr;
    }
    my_vct(const my_vct &other)
    {
        int len=other.last-other.first;
        int size=other.end-other.first;

        this->first=new T[size];
        memcpy(this->first,other.first,len*sizeof (T));

        this->last=this->first+len;
        this->end=this->first+size;
    }

    bool my_empty()
    {
        return this->first==this->last;
    }

    bool my_full()
    {
        return this->first==this->end;
    }

    void my_greater()
    {
        int size=this->end-this->first;
        T *temp=new T[2*size];
        memcpy(temp,this->first,size*sizeof(T));

        delete []first;
        first=temp;
        last=first+size;
        end=first+2*size;
    }

    void my_pushback(const T val)
    {
        if(this->my_full())
            this->my_greater();
        *last=val;
        last++;
    }

    void my_popback()
    {
        if(this->my_empty())
            return;
        --last;
    }

    T&front()
    {
        return  *first;
    }

    int size()
    {
        return end-first;
    }

    int len()
    {
        return last-first;
    }

    T&at(int pos)
    {
        if(pos<0||pos>this->len())
            cout<<"访问越界,不合法 "<<endl;
        return first[pos];
    }


};


int main()
{
    my_vct< int > my(10);
    for(int i=0; i<20; i++)
    {
        my.my_pushback(i);
        cout<<my.size()<<endl;
    }
    for(int i=0;i<20;i++)
    {
        cout<<my.at(i)<<endl;
    }


    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值