手动实现My_vector

作业:手动实现My_vector

代码如下:

#include <iostream>

using namespace std;

template<typename T>
class Myvector
{
private:
    T *front;
    T *last;
    T *end;
public:
    //无参构造
    Myvector();
    //有参构造
    Myvector(T d,int len);
    //拷贝构造
    Myvector(const Myvector &r);
    //拷贝赋值
    Myvector<T> &operator=(const Myvector &other);
    //析构函数
    ~Myvector();
    //at函数
    T at(int a);
    //empty函数
    bool empty();
    //full函数
    bool full();
    //tfront函数
    T tfront();
    //back函数
    T &back();
    //size函数
    int size();
    //push_back函数
    void push_back(T data);
    //pop_back函数
    void pop_back();
    //2倍扩容函数
    void expand();
    //展示函数
    void show();


};

template <typename T>

Myvector<T>::Myvector()
{
    front = last = end = nullptr;
    cout<<"无参构造"<<endl;
}

template <typename T>
//有参构造
Myvector<T>::Myvector(T d,int len)
{
    front = new T[len];
    for(int i=0;i<len;i++)
    {
        *(front+i) = d;
    }
    last = end = front+len;
    cout<<"有参构造"<<endl;
}

template <typename T>
//拷贝构造
Myvector<T>::Myvector(const Myvector &r)
{
    front = new T[r.end-r.front];
    memcpy(front,r.front,sizeof((r.end-r.front)*sizeof(T)));
    last = front+(r.last-r.front);
    end = front+(r.end-r.front);
    cout<<"拷贝构造"<<endl;
}

template <typename T>
//拷贝赋值
Myvector<T> & Myvector<T>::operator=(const Myvector &other)
{
    if((end-front)<(other.end-other.front))
    {
        delete front;
        front = nullptr;
        front = new T[other.end-other.front];
        end = front+(other.end-other.front);
    }
    memcpy(front,other.front,((other.end-other.front)*sizeof(T)));
    last = front+(other.last-other.front);
    cout<<"拷贝赋值"<<endl;
    return *this;
}

template <typename T>
//析构函数
Myvector<T>::Myvector::~Myvector()
{
    delete []front;
    front = nullptr;
    cout<<"析构函数"<<endl;
}

template <typename T>
//at函数
T Myvector<T>::Myvector::at(int a)
{
    if(a<0 || a>(last-front-1))
    {
        cout<<"所给数据不合理"<<endl;
    }
    return *(front+a);
}

template <typename T>
//empty函数
bool Myvector<T>::Myvector::empty()
{
    if(front == last)
    {
        return true;
    }
    else
    {
        return false;
    }
}

template <typename T>
//full函数
bool Myvector<T>::full()
{
    if(last == end)
    {
        return true;
    }
    else
    {
        return false;
    }
}

template <typename T>
//tfront函数
T Myvector<T>::tfront()
{
    if(empty())
    {
        cout<<"数据为空"<<endl;
    }
    return *front;
}

template <typename T>
//back函数
T &Myvector<T>::back()
{
    if(empty())
    {
        cout<<"数据为空"<<endl;
    }
    return *(last-1);
}

template <typename T>
//size函数
int Myvector<T>::size()
{
    return last-front;
}

template <typename T>
//push_back函数
void Myvector<T>::Myvector::push_back(T data)
{
    if(last != end)
    {
        *last = data;
        last++;
        cout<<"push1"<<endl;
    }
    else if(last == end && !empty())
    {
        expand();
        cout<<"二倍扩容"<<endl;
        *last = data;
        last++;
        cout<<"push"<<endl;

    }
    else if(empty())
    {
        front = new T;
        *front = data;
        last++;
        end = last;
        cout<<"push2"<<endl;
    }
    cout<<"push3"<<endl;


}

template <typename T>
//pop_back函数
void Myvector<T>::pop_back()
{
    if(empty())
    {
        cout<<"数据为空"<<endl;
        return;
    }
    last--;
}

template <typename T>
//2倍扩容函数
void Myvector<T>::expand()
{
int *temp;
temp = new T[(end-front)*2];
memcpy(temp,front,((end-front)*sizeof(T)));
last = temp+(end-front);
end = temp+((end-front)*2);
delete front;
front = nullptr;
front = temp;

cout<<"二倍扩容"<<endl;
}

template <typename T>
//展示函数
void Myvector<T>::show()
{
    for(int i=0;(front+i)!=last;i++)
    {
        cout<<*(front+i);
    }
    cout<<endl;
}

int main()
{
    Myvector<int> m1(5,5);
    m1.show();
    m1.push_back(7);
    m1.show();
    m1.push_back(7);
    m1.push_back(7);
    m1.push_back(7);
    m1.push_back(7);
    m1.push_back(7);
    m1.push_back(7);
    m1.push_back(7);
    m1.show();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大鱼YY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值