c++&qt第四次作业

该文章展示了一个用C++实现的名为Myvector的模板类,它具有动态数组的功能,包括无参构造、有参构造、拷贝构造、拷贝赋值、析构函数、以及一些基本操作如at、empty、full、front、back、size、capacity、clear、expand、push_back、pop_back等方法。代码中还包含了示例用法,展示了如何创建、赋值、操作Myvector对象。
摘要由CSDN通过智能技术生成
#include <iostream>
#include <cstring>

using namespace std;

template <typename T>

class Myvector
{
private:
    T *first;//首地址
    T *last;//最后一个元素的下一地址
    T *end;//容器尾地址
public:
    //无参构造
    Myvector():first(nullptr),last(nullptr),end(nullptr){}

    //有参构造
    Myvector(int num,const T &val )
    {
        first = new T[num];
        last=first;
        end=first+num-1;
        for(int i=0;i<num;i++)
        {
            first[i]=val;
            last++;
        }
    }

    //拷贝构造
    Myvector(const Myvector &other)
    {
        first = new T[other.end-other.first+1];
        last=first;
        end=first+(other.end-other.first);
        for(int i=0;i<(other.last-other.first);i++)
        {
            first[i]=other.first[i];
            last++;
        }
    }

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

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

    //at()
    T &at(int pos)
    {
        if(pos<0||pos>(last-first-1))
        {
            cout<<"访问下标不合法"<<endl;
            exit(0);
        }
        return first[pos];
    }

    //判空
    bool empty()
    {
        return last==first;
    }

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

    //front()
    T &front()
    {
        return *first;
    }

    //back()
    T &back()
    {
        return *(last-1);
    }

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

    int capacity()
    {
        return end-first+1;
    }

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

    //二倍扩容
    void expand()
    {
        T *temp=new T[(this->end-this->first+1)*2];
        this->last=temp;
        for(int i=0;i<(this->end-this->first);i++)
        {
            temp[i]=this->first[i];
            last++;
        }
        this->end=temp+(this->end-this->first+1);
        delete []first;
        this->first=temp;
        temp=nullptr;
    }

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

    //pop_back
    void pop_back()
    {
        if(empty())
        {
            cout<<"容器已空"<<endl;
            exit(0);
        }
        last--;
    }
    //begin()
     const T *begin() const {
         return first;
     }

     //end()
     const T *pend() const {
         return last;
     }

     //成员函数实现[]符号重载
     const T &operator[](const int pos) const
     {
         return first[pos];
     }


};

int main()
{

    Myvector<int> v1(5,8);
    Myvector<int> v2(v1);

    for (int i = 0; i < v1.size(); i++)
    {
        cout << "v1[" << i << "] = " << v1[i] << endl;
    }

    cout<<"****************"<<endl;

    for (int i = 0; i < v2.size(); i++)
    {
        cout << "v2[" << i << "] = " << v2[i] << endl;
    }

   cout<<"****************"<<endl;

    //赋值构造函数
    Myvector<int> v3;
    v3 = v1;
    for (int i = 0; i < v3.size(); i++)
    {
        cout << "v3[" << i << "] = " << v3[i] << endl;
    }
    cout << "**********************" << endl;

    //清空v3
    v3.clear();
    cout << "v3.size() = " << v3.size() << endl;
    cout << "*********************" << endl;

    //判空
    if (v3.empty())
        cout << "v3容器已空!" << endl;
    else
        cout << "v3容器非空!" << endl;
    cout << "************************" << endl;

    //v3尾插
    v3.push_back(1);
    v3.push_back(2);
    v3.push_back(3);

    for (int i = 0; i < v3.size(); i++)
    {
        cout << "v3[" << i << "] = " << v3[i] << endl;
    }
    cout << "************************" << endl;

    //第一个元素
    cout << "v3.front = " << v3.front() << endl;

    //最后一个元素
    cout << "v3.back = " << v3.back() << endl;


    cout << "************************" << endl;

    //尾删
    v3.pop_back();

    for (int i = 0; i < v3.size(); i++)
    {
        cout << "v3[" << i << "] = " << v3[i] << endl;
    }


    return 0;
}

结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值