template<typename T>
class Myvector
{
private:
T* first;
T* last;
T* end;
public:
Myvector(){};
Myvector(int num,T e);
Myvector(const Myvector& from);
~Myvector();
Myvector& operator=(const Myvector& from);
T& at(int loc);
T& front();
T& back();
bool empty();
bool full();
int size();
void clear();
T* expand();
void push_back(T e);
void pop_back();
};
template<typename T>
Myvector<T>::Myvector(int num,T e)
{
first=new T[num];
last=first;
end=first+num;
for(int i=0;i<num;i++)
{
*(first+i)=e;
last++;
}
}
template<typename T>
Myvector<T>::Myvector(const Myvector<T>& from)
{
int num=from.end-from.first;
first=new T[num];
last=first;
end=first+num;
for(int i=0;i<num;i++)
{
*(first+i)=*(from.first+i);
last++;
}
}
template<typename T>
Myvector<T>::~Myvector()
{
delete[] first;
first=nullptr;
last=nullptr;
end=nullptr;
}
template<typename T>
Myvector<T>& Myvector<T>:: operator=(const Myvector<T>& from)
{
delete[] first;
int num=from.end-from.first;
first=new T[num];
last=first;
end=first+num;
for(int i=0;i<num;i++)
{
*(first+i)=*(from.first+i);
last++;
}
}
template<typename T>
T& Myvector<T>::at(int loc)
{
try {
if(first+loc>last)
throw(1);
else
return *(first+loc);
} catch (1) {
cout<<"out of range"<<endl;
}
}
template<typename T>
bool Myvector<T>::empty()
{
if(first==last)
return true;
else
return false;
}
template<typename T>
bool Myvector<T>::full()
{
if(end==last)
return true;
else
return false;
}
template<typename T>
T& Myvector<T>::front()
{
try {
if(empty())
throw(1);
else
return *first;
} catch (1) {
cout<<"list is empty"<<endl;
}
}
template<typename T>
T& Myvector<T>::back()
{
try {
if(empty())
throw(1);
else
return *last;
} catch (1) {
cout<<"list is empty"<<endl;
}
}
template<typename T>
int Myvector<T>::size()
{
return last-first;
}
template<typename T>
void Myvector<T>::clear()
{
last=first;
}
template<typename T>
T* Myvector<T>::expand()
{
int num=end-first;
first=memcpy(first,first,2*sizeof(T)*num);
end=first+2*num;
}
template<typename T>
void Myvector<T>::push_back(T e)
{
if(full())
expand();
*last=e;
last++;
}
template<typename T>
void Myvector<T>::pop_back()
{
try {
if(empty())
throw(1);
else
last--;
} catch (1) {
cout<<"list is empty"<<endl;
}
}