//vector class
#include <iostream>
using namespace std;
class CMyVector {
private:
int *m_pDatas;
int m_size;
int m_bufferSize;
public:
CMyVector ();
CMyVector (int n);
CMyVector (const CMyVector &);
int size () const {
return m_size;
}
void push_back (int x);
CMyVector & operator = (const CMyVector &);
~CMyVector () {
delete [] m_pDatas;
}
int & operator [] (int index) const;
};
CMyVector::CMyVector ()
: m_size (0), m_bufferSize (0), m_pDatas (NULL)
{}
CMyVector::CMyVector (int n)
:m_size (n), m_bufferSize (n)
{
m_pDatas = new int [m_bufferSize];
}
CMyVector::CMyVector (const CMyVector &rhs)
: m_size (rhs.m_size), m_bufferSize (rhs.m_bufferSize), m_pDatas (NULL)
{
if (m_bufferSize > 0) {
m_pDatas = new int [m_bufferSize];
for (int i = 0; i < m_size; i++)
m_pDatas [i] = rhs.m_pDatas [i];
}
}
void CMyVector::push_back (int x)
{
if (m_size >= m_bufferSize) {
int *pBuffer = new int [m_bufferSize + 10];
if (m_size > 0)
memcpy (pBuffer, m_pDatas, m_size* sizeof (int));
delete [] m_pDatas;
m_pDatas = pBuffer;
m_bufferSize += 10;
}
m_pDatas [m_size++] = x;
}
CMyVector & CMyVector::operator = (const CMyVector &rhs)
{
if (this == &rhs)
return *this;
delete [] m_pDatas;
m_size = rhs.m_size;
m_bufferSize = rhs.m_bufferSize;
m_pDatas = NULL;
if (m_bufferSize > 0) {
m_pDatas = new int [m_bufferSize];
for (int i = 0; i < m_size; i++)
m_pDatas [i] = rhs.m_pDatas [i];
}
return *this;
}
int& CMyVector::operator [] (int index) const
{
if (index >= 0 && index < m_size)
return m_pDatas [index];
throw "Overflow";
}
void Print (const CMyVector &V)
{
for (int i = 0; i < V.size (); i++) {
cout << V[i] <<"\t";
}
cout << endl;
}
int main ()
{
try {
int n;
cin >> n;
CMyVector V1 (n);
for (int i = 0; i < V1.size (); i ++) {
V1 [i] = 150 * i;
}
V1.push_back (100);
V1.push_back (200);
CMyVector V2 (V1);
V2.push_back (300);
CMyVector V3;
V3 = V1;
V3.push_back (400);
Print (V1);
Print (V2);
Print (V3);
CMyVector V4;
V4 [4] = 5;
cout << V4 [6] << endl;
cout << "Finish" << endl;
}
catch (int no) {
cout << "Error No :" << no << endl;
}
catch (char *errMsg) {
cout << errMsg << endl;
}
catch (...) {
}
cout << "complete" << endl;
return 0;
}
