#include <iostream>
using namespace std;
template <typename T>
class Myvector
{
private:
T* first;
T* last;
T* end;
public:
//无参构造
Myvector():first(0),last(0),end(0){}
//有参构造
Myvector(int n,const T& val):first(new T[n])
{
for(int i=0;i<n;i++)
{
first[i]=val;
}
last=end=first+n;
}
//析构
~Myvector()
{
delete []first;
}
//拷贝构造
Myvector(const Myvector&F):first(new T[F.end-F.first])
{
last=first+(F.last-F.first);
end=first+(F.end-F.first);
memcpy(first,F.first,(last-first)*sizeof(T));
}
//拷贝赋值
Myvector& operator=(const Myvector& F)
{
delete []first;
first=new T[F.last-F.first];
last=first+(F.last-F.first);
end=first+(F.end-F.first);
memcpy(first,F.first,(last-first)*sizeof(T));
return *this;
}
//at
T&at(int pos)
{
if(pos<0||pos>=last-first)
{
throw range_error("pos");
}
return first[pos];
}
//empty
bool empty()
{
return last==first?true:false;
}
//full
bool full()
{
return last==end?true:false;
}
//size
int size()
{
return last-first;
}
//clear
void clear()
{
last==first;
}
//扩容
void kuo()
{
Myvector temp;
temp.first=new T[(end-first)*2];
delete []first;
}
//push_back 在最后添加一个元素
void push_back(const T&F)
{
if(full())
{
kuo();
}
*last++=F;
}
//pop_back 移出最后一个元素
void pop_back()
{
if(empty())
throw;
last--;
}
//front
T&front()
{
return *first;
}
//back
T&back()
{
return *(end-1);
}
void show()
{
for(auto i=this->first;i<this->last;i++)
{
cout <<*i << " ";
}
cout<<endl;
}
};
int main()
{
Myvector<int> t(5,2);
t.show();
Myvector<int> T=t;
T=t;
T.show();
T.push_back(2);
T.show();
T.pop_back();
T.show();
return 0;
}