#include<iostream>
#include<stack>
using namespace std;
template<typename _Ty>
class Vector
{
public:
typedef _Ty value_type;
public:
Vector()
{
parr = new _Ty[2]();
cursize = 0;
totalsize = 2;
}
~Vector()
{
delete[] parr;
parr = NULL;
}
void push_back(const value_type& val)
{
insert(cursize, val);
}
void insert(int pos, const value_type& val)
{
if (pos < 0 || pos > cursize)
{
throw std::exception("pos is error!");
}
if (full())
{
resize();
}
int index = cursize;
for (index; index > pos; index--)
{
parr[index] = parr[index - 1];
}
parr[pos] = val;
cursize++;
}
bool empty()
{
return cursize == 0;
}
void pop_back()
{
erase(cursize - 1);
}
void erase(int pos)
{
if (pos < 0 || pos >= cursize)
{
throw std::exception("pos is error!");
}
if (empty())
{
throw std::exception("vector is empty!");
}
int index = pos;
for (index; index < cursize - 1; index++)
{
parr[index] = parr[index + 1];
}
cursize--;
}
_Ty back()
{
if (empty())
{
throw std::exception("vector is empty!");
}
return parr[cursize - 1];
}
void show()
{
int index = 0;
for (index; index < cursize; index++)
{
std::cout << parr[index] << " ";
}
std::cout << std::endl;
}
private:
void resize()
{
_Ty* pnewarr = new _Ty[totalsize * 2]();
memcpy(pnewarr, parr, sizeof(_Ty)*totalsize);
delete[] parr;
parr = pnewarr;
totalsize *= 2;
}
bool full()
{
return cursize == totalsize;
}
_Ty* parr;
int cursize;//
int totalsize;
};
int main()
{
return 0;
}