C++&QT 作业7

C++&QT 作业7_第1张图片

#include 

using namespace std;
#include 
#include 
using namespace std;

template 
class myvector{
private:
    T *first;
    T *last;
    T *end;
public:
    //无参构造
    myvector(){
        first = new T[1];
        last = first;
        end = first+1;
    }

    //有参构造
    myvector(int num,const T &val){
        first = new T[num + 1];
        last = first;
        end = first + num;
        for(int i=0;i *other){
        int len = other->end - other->first+1;
        this->first = new T[len];
        this->last = other->last;
        this->end = other->end;
        for(int i=0;ifirst[i] = other->first[i];
            ++last;
        }
        end += len-1;
    }

    //拷贝赋值
    myvector &operator = (const myvector *other){
        if(this!=&other){
            delete []first;
            int len = other->end - other->first+1;
            this->first = new T[len];
            this->last = other->last;
            this->end = other->end;
            for(int i=0;ifirst[i] = other->first[i];
            }
        }
        return *this;
    }

    //析构函数
    ~myvector(){
        delete []first;
        first = nullptr;
        last = nullptr;
        end = nullptr;
    }

    //判空
    bool empty(){
        if(last == first)
            return true;
        else
            return false;
    }

    //判满
    bool full(){
        return last == end;
    }

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

    //尾删
    void pop_back(){
        if(empty()){
            throw -1;
        }
        last--;
    }

    //二倍镜扩容
    void expand(){
        int t = last-first;
        T *temp = new T[2*(end-first)];
        for(int i=0;iend-first){
            throw -1;
        }
        return first[pos];
    }

    //返回首元素
    T &front(){
        return *first;
    }

    //返回尾元素
    T &back(){
        return *(end-1);
    }

    //求元素个数
    int size()const{
        return last-first;
    }

    //清空
    void clear(){
        last = first;
    }

    //遍历
    void show(){
        for(int i=0;i v1(4,8);
    v1.show();
    v1.push_back(5);
    v1.show();
    myvector v2(v1);
    cout<<"v2长度为"<

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