#include <iostream>
using namespace std;
template<typename T>
class Myvector
{
private:
T* first;//指向头
T* last; //指向空间尾
T* end; //指向实际长度尾
public:
//无参构造
Myvector(){}
//有参构造
Myvector(size_t size,const T& val)
{
first=new T[size];
last=first+size-1;//偏移到空间尾部
T* t=first;//防止first偏移
//遍历
for(size_t i=0;i<size;i++)
{
*(t+i)=val;
}
end=t;//便宜到实际长度尾部
}
//析构函数
~Myvector()
{
//释放空间
delete []first;
//全部指空
first=nullptr;
end=nullptr;
last=nullptr;
}
//拷贝构造
Myvector(const Myvector & v)
{
//判断v是否空
if(nullptr==v.first)
{
first=nullptr;
last=nullptr;
end=nullptr;
}
else
{
int v_size=v.last-v.first+1;
first=new T[v_size];
end=first+v.end-v.first;
last=first+v_size-1;
memcpy(first,v.first,sizeof(T)*v_size);//memcpy(目标地址,源地址,大小);
}
}
//拷贝赋值
Myvector<T> &operator=(const Myvector &other)
{
int size1=other.last-other.first+1;//空间大小
int size2=other.end-other.first;//实际大小
//没有空间 没有内存
if(other.end == other.first && first==nullptr)
{
//全部指空
first = nullptr;
last = nullptr;
end = nullptr;
}
//有空间 没内存
else if(other.end == other.first)
{
delete []first;
first = new T[size1];
end = first;
last = first+size1-1;
}
//既有空间也有内存
else
{
delete []first;
first = new T[size1];
memcpy(first,other.first,sizeof(T)*size1);
end = first + size2;
last = first + size1-1;
}
return *this;
}
//at()
T &at(int pos)
{
return *(first+pos);
}
//empty()
bool empty()
{
if(nullptr==first||end==first)
{
return true;
}
else
{
return false;
}
}
//full()
bool full()
{
if(end==last)
{
return true;
}
else
{
return false;
}
}
//front()
T & front()
{
return *first;
}
//back()
T & back()
{
return *(end-1);
}
//size()
size_t size()
{
if(nullptr==first)
{
return 0;
}
else
{
return end-first;
}
}
//clear()
void clear()
{
memset(first,0,sizeof(T)*(last-first+1));
end=first;
}
//expand()
void expand()
{
if(nullptr==first)
{
first=new T;
end=first;
last=first;
}
else
{
size_t length=end-first;//实际大小
size_t size=last-first+1;//空间大小
T* temp=new T[2*size];
memcpy(temp,first,sizeof(T)*length);
delete []first;//释放原有空间
first=temp;
end=first+length;
last=first+(2*size)-1;
}
}
//push_back()
void push_back(T &val)
{
//判空
if(nullptr==first)
{
first=new T[2];
*first=val;
end=first+1;
last=first+1;
}
else
{
if(end==last)
{
this->expand();//二倍扩容
}
*end=val;//赋值
end++;
}
}
//pop_back()
void pop_back()
{
//判空
if(nullptr==first||first==end)
{
return;
}
else
{
end--;
}
}
void show()
{
cout<<"output:"<<endl;
for(int i=0;i<(end-first);i++)
{
cout<<at(i)<<'\t';
}
cout<<endl<<endl;
}
};
int main()
{
Myvector<int> v(1,0);//显示调用myvector模板类,有参构造初始化对象v
//判空
cout<<"empty:"<<boolalpha<<v.empty()<<endl<<endl;
cout<<"input:"<<endl;
//循环输入
for(int i=0;i<5;i++)
{
cout<<"full:"<<boolalpha<<v.full()<<endl;//判满
cout<<endl;
v.push_back(i);
}
//遍历
v.show();
//实际长度
cout<<"size after push_back():"<<v.size()<<endl<<endl;;
//首元素
cout<<"front:"<<v.front()<<endl<<endl;
//尾元素
cout<<"back:"<<v.back()<<endl<<endl;
//尾删
v.pop_back();
//尾删之后的size
cout<<"size after once pop_back():"<<v.size()<<endl<<endl;
//遍历
v.show();
Myvector<int> vv(1,0);
//拷贝赋值
vv=v;
cout<<"after copy"<<" ";
vv.show();
return 0;
}
————————————————
版权声明:本文为CSDN博主「kcncoooo」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/kcncoooo/article/details/130375668
封装一个Myvector
于 2023-05-16 00:09:01 首次发布