C++&QT 作业7

#include <iostream>

using namespace std;
#include <iostream>
#include <vector>
using namespace std;

template <typename T>
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<num;i++){
            first[i] = val;
            last++;
        }
    }

    //拷贝构造
    myvector(const myvector<T> *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;i<len-1;i++){

            this->first[i] = other->first[i];
            ++last;
        }
        end += len-1;
    }

    //拷贝赋值
    myvector &operator = (const myvector<T> *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;i<len-1;i++){

                this->first[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;i<last-first;i++){
            temp[i] = first[i];
        }
        delete []first;
        first = temp;
        last = first+t;
        end = first + 2*(end-first);
        temp = nullptr;
    }

    //at函数
    T &at(int pos){
        if(pos<0||pos>end-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<size();i++){
            cout<<first[i]<<" ";
        }
        cout<<endl;
    }

};

int main()
{
    myvector<int> v1(4,8);
    v1.show();
    v1.push_back(5);
    v1.show();
    myvector<int> v2(v1);
    cout<<"v2长度为"<<v2.size()<<endl;
    v2.pop_back();
    cout<<"尾删后v2长度为"<<v2.size()<<endl;
    v2.clear();
    cout<<"清空后v2长度为"<<v2.size()<<endl;

    return 0;

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值