7-24_homework

#include <iostream>

using namespace std;

template <typename T>
class myvector{
private:
T *first;
T *last;
T *end;

public:
//有参
myvector():first(nullptr),last(nullptr),end(nullptr){}

//析构
~myvector(){}

//拷贝构造
myvector(const myvector & other){
int len=other.size();
first=new T [len];
last=first+len;
end=last;    //初始化end=last
}

//拷贝赋值
myvector & operator = (const myvector & other){
if(this!=other){
int len =other.size();
first=new T[len];
last=first+len;
end=last;
}
return *this;
}

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

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

//获取指定下标
T &at(int index){
return first[index];
}

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

//返回最后一个元素
T & tail(){
return *(last-1);
}

//返回容器大小  size
int size(){
return last-first;
}

//清理容器
void clear(){
return last=first;
}

//二倍扩容
void expand(){
int length = size();
int newvector = length*2;   //扩容
T *newfirst = new T[newvector]; //创建空间
T *newlast = newfirst + length;
T *newend = newfirst + newvector;
for(int i=0;i<length;i++)   //赋值
{
    newfirst[i] = first[i];
}
delete []first;     //释放
first = newfirst;
last = newlast;
}

//尾插
void push_back(const T &key){
*last=key;
last++;
if(last>end)  end=last;
}

//尾删
void pop_back(){
last--;
}

};



int main()
{
        myvector<int> m1;
        //插入
        for (int i = 0; i < 20; i++)
        {
            m1.push_back(6666);
        }

        //输出
        for(int i=0;i<m1.size();i++)
        {
            cout<<m1.at(i)<<"  ";
        }
        cout<<endl;

        //删除
        m1.pop_back();

        for(int i=0;i<m1.size();i++)
        {
            cout<<m1.at(i)<<"  ";
        }
        cout<<endl;

        return 0;

}

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值