C++:封装一个Vector容器

 

#include <iostream>

using namespace std;
template <typename T>

class Myvector
{
    private:
        T * first;
        T * last;
        T * end;

    public:
            //构造函数
        Myvector(int size = 10)
        {
            this->first = new T[size];
            this->last = this->first;
            this->end = this->first + size;
        }
            //析构函数
        ~Myvector()
        {
            delete []first;
        }
            //拷贝构造
        Myvector(const Myvector& other)
        {
            int size = other.end - other.first;
            this->first = new T[size];
            int len = other.last - other.first;

            memmove(this->first,other.first,len*sizeof(T));
            this->last = this->first + len;
            this->end = this->first + size;

            return *this;
        }
            //拷贝赋值
        Myvector& operator = (const Myvector &other)
        {
            int size = other.end - other.first;
            if(this->first != nullptr)
            {
                delete []this->first;
                this->first = new T[size];
            }else
            {
                this->first = new T[size];
            }
            int len = other.last - other.first;
            memmove(this->first,other.first,len*sizeof(T));
            this->last = this->first +len;
            this->end= this->first + size;
            return  *this;
        }
            //T &at(int index)
        T &at(int index)
        {
            for(int i=*first;i<*last;i++)
            {
                if(first[i]=index)
                return first[i];
            }

        }


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

            //full()
        bool full()
        {
            return this->end == this->last;
        }

            //front()
        T& front()
        {

            return *this->first;
        }


            //back()
        T& back()
        {

            return  *this->last;
        }


            //size()
        int size()
        {
            return this->last-this->first;
        }

            //clear()
        void clear()
        {
            delete []first;
            this->first = nullptr;
        }

            //expand()     二倍扩容函数
        void expand()
        {
            int size = this->last-this->first;
            T*temp = new T[2*size];
            memcpy(this->temp,this->first,sizeof(T)*(this->end - this->first));
            delete []first;
            this->first = this->temp;
            this->last = this->first + size;
            this->end = this->first + 2*size;

            return *this;
        }

            //push_back()
        void push_back(const T& value)
        {
            if(full())
            {
                expand();
            }
            *last++ = value;

        }

            //pop_back()
        void pop_back()
        {
            if(empty())
            {
                return;
            }
            *last--;
        }


};


int main()
{
    cout << "Hello World!" << endl;
    return 0;
}

 思维导图:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值