C++作业day7

 

#include <iostream>
#include<cstring>
using namespace std;
template <typename T>
class Myvector
{
private:
    T *first=nullptr;
    T *last=nullptr;
    T *end=nullptr;
public:
    T data;
    T size;
public:
    Myvector()
    {
        cout<<"无参构造"<<endl;
    }        //无参构造
    Myvector(int i,T d)
    {
        first=new T[i];
        for(int a=0;a<i;a++)
        {
            first[a]=d;
        }
        last=first+i;
        end=first+i;
        cout<<"有参构造"<<endl;
    }//有参构造
    ~Myvector() //析构函数
    {
        delete[]first;
        first=last=end=nullptr;
        cout<<"析构函数"<<endl;
    }
    Myvector(const Myvector &other):first(new T(*(other.first))),last(new T(*(other.last))),end(new T(*(other.end)))
    {
        cout<<"拷贝构造"<<endl;
    }//拷贝构造
    Myvector &operator=(const Myvector &other)
    {
        int len = other.last-other.first;
        int size = other.end-other.first;
        first = new T[size];
        for(int i=0;i<len;i++)
        {
            first[i] = other.first[i];
        }
        last = first + len;
        end = first + size;
        cout<<"拷贝赋值函数"<<endl;
        return *this;
    }//拷贝赋值

    //at函数
    T &at(int pos)
    {
        if(pos>=size||pos<0)
            cout<<"不存在"<<endl;
        return  first[pos];
    }

    bool empty()//判空
    {
        if(last==0)
            cout<<"empty"<<endl;
        else
            cout<<"not empty"<<endl;
    }

    bool full()//判满
    {
        if(last==end)
        {
            cout<<"full"<<endl;
        }else
            cout<<"not full"<<endl;
    }
    T front()//第一个元素
    {
        cout<<"第一个元素"<<*first<<endl;
    }
    T back()//最后一个元素
    {
        cout<<"最后一个元素"<<*last<<endl;
    }
    void size_t()//大小
    {
        cout<<"size = "<<*last<<endl;
    }
    void clear()//清空
    {
        while (last!=first)
        {
            pop_back();

        }
    }
    void expand()//二倍扩容函数
    {
        int len = end-first;
        T *temp = new T(2*len);
        memcpy(temp,first,sizeof(T)*len);
        delete []first;
        first = temp;
        last = first+len;
        end = first + 2*len;
    }
    void push_back(T d)//尾插
    {
        last=last+1;
        *last=d;
        cout<<"尾插成功"<<endl;
    }
    void pop_back()
    {
        //判空
        if(empty() == true)
        {
            cout<<"删除失败,该链表为空"<<endl;
            return ;
        }
        last--;
        cout<<"删除成功"<<endl;
    }
    void bianli()//遍历
    {
        for(int i=0;i<*last;i++)
        {
            cout<<first[i]<<"   ";
        }
        cout<<endl;
    }
};
int main()
{
    Myvector<int> a1(6,3);
    Myvector<int> a2(a1);
    Myvector<int> a3;
    a3=a1;
    a1.push_back(4);
    a1.pop_back();
    a1.front();
    //a1.at(2);
    a1.back();
    a1.full();
    a1.empty();
    a1.size_t();
    // a1.expand();
    a1.bianli();
    a1.clear();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值