#include <iostream>
#include<vector>
using namespace std;
template <typename T>
class Myvector
{
public:
T* start;
T* last;
T* end;
public:
Myvector():start(new T [10]),last(start),end(start+10)
{}
Myvector(size_t num , const T &val)
{
this->start = new T [num];
this->last = this->start;
this->end = this->last +num;
for(auto i = 0 ; i < num; i++)
{
start[i] = val;
this->last++;
}
}
Myvector(const Myvector &other)
{
this->start = new T (other.end-other.start);
this->last = this->start+(other.last - other.start);
this->end = this->start +(other.end-other.start);
int i = 0;
for (T* p = this->start ; p <= this->end; p++)
{
p[0] = other.start[i];
i++;
}
}
Myvector &operator =(const T& other)
{
if(this != other)
{
this->start = new T (other.end-other.start);
this->last = this->start+(other.last - other.start);
this->end = this->start +(other.end-other.start);
int i = 0;
for (T* p = this->start ; p <= this->end; p++)
{
p[0] = other.start[i];
i++;
}
}
return *this;
}
~Myvector()
{
delete []start;
start =nullptr;
last =nullptr;
end = nullptr;
}
T at(int i)
{
if(i<0||i>(this->last-this->start)) //i>(this->last-this->start)/sizeof (T)
{
throw string ("error");
}
else
{
return *(this->start+i);
}
}
bool empty()
{
return this->last==this->start?true:false;
}
bool full()
{
return this->last==this->end?true:false;
}
T & front()
{
return *this->start;
}
T & back()
{
return *(this->last-1);
}
T size()
{
return this->last - this->start;
}
void clear()
{
this->last = this->start;
this->end = this->start+1;
}
void expand()
{
T size = end-start;
T * temp = new T (2*(end-start));
memcpy(temp,start,sizeof (T)*(end-start));
delete []start;
start = temp;
last = start +size;
end = start +2*size;
}
void push_back(const T &data)
{
if(this->full())
{
this->expand();
}
*(this->last) = data;
last++;
}
void pop_back()
{
if(this->empty())
{throw string ("error");}
else
{
last --;
}
}
};
int main()
{
Myvector <int> v1;
Myvector <int> v2 (1,5) ;
Myvector <int> v3(v2) ;
v3.push_back(9);
try {
cout<<v3.at(3)<<endl;
} catch (string msg) { cout<<msg<<endl;
}
cout<<"v3.back() = "<<v3.back()<<endl;
cout <<"v3.size() = "<< v3.size() <<endl;
v3.pop_back();
cout<<"v3.front() ="<<v3.front()<<endl;
cout<<"v3.back() ="<<v3.back()<<endl;
return 0;
}