#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;
}