QT- Day 1:

QT- Day 1:_第1张图片

 QT- Day 1:_第2张图片

#include 

using namespace std;

template

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

public:
    //无参构造
    Myvector(){cout<<"无参构造"<end=this->first+size;
        this->last=this->first;
        cout<<"有参构造"<first!=this->end)
        {
            //  delete first;
            //  this->first=this->last=this->end=nullptr;
            cout<<"析构函数"<first,other.first,len*sizeof(T));

         //更新指针
         this->last=len+this->first;
         this->end=this->first+size;

         cout<<"拷贝构造"<first,other.first,len*sizeof(T));

        //更新指针
        this->last=len+this->first;
        this->end=this->first+size;
        
        cout<<"拷贝赋值"<end-this->first;
        if(pos<0||pos>size)
        {
            cout<<"位置非法"<first==this->last)
       {
           return true;
       }
       else{
           return false;
       }
    }

    //判满
    bool full()
    {
        if(this->end==this->last)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //front()函数返回当前vector起始元素的引用
    T &front()
    {
        return first[0];
    }

    //返回最末一个元素
    T &back(){
        return end[0];
    }

    //返回Vecor元素数量的大小
    T &size()
    {
        return this->last-this->first;
    }

    //clear 清空所有元素
    void clear()
    {
       int size=this->last-this->first;
       for(int i=0;ilast-this->first;
        if(this->last>=this->end)
        {
            //二倍扩容
            T *p=this->first;
            this->first=new T [size*2];
            memcpy(this->first,p,size*2);
            this->end=this->first+2*size;
        }
    }

    //在vector最后添加一个元素
    void push_back(T p)
    {
        if(full())
        {

            expand();
        }
        *(this->last)=p;
         this->last++;
    }

    //移除最后一个元素
    void pop_back()
    {
        this->last--;
    }

    //遍历容器中数据
    void show()
    {
        int size=this->last-this->first;
        for(int i=0;i s(5);
    //调用拷贝构造
    Myvector s1(s);
    //调用拷贝赋值 
    Myvector s2;
    s2=s;

    //添加元素
    s2.push_back(1314);
    s2.push_back(520);
    s2.push_back(666);
    s2.show();

    //front()函数
    cout<<"开始元素="<

你可能感兴趣的:(qt,c++,算法)