#include <iostream>
#include <iomanip>
#include <vector>
#include <new>
using namespace std;
template <class T>
class SimpleVector
{
private:
T* aptr;
T* bptr;
int arraySize;
void memError()
{
cout<<" Memory allocation error!"<<endl;
exit(54);
}
void subError()
{
cout<<" Subscript out of range!"<<endl;
exit(54);
}
public:
SimpleVector()
{
aptr = 0;
arraySize = 0;
}
SimpleVector(int s)
{
arraySize = s;
try
{
aptr = new T [s];
}
catch (bad_alloc)
{
memError();
}
for (int count = 0; count < arraySize; count++)
{
*(aptr + count) = 0;
}
}
SimpleVector(const SimpleVector &obj)
{
arraySize = obj.arraySize;
aptr = new T [arraySize];
if(aptr==0)
{
memError();
}
for(int count=0; count<arraySize; count++)
{
*(aptr + count) = (obj.aptr + count);
}
}
~SimpleVector()
{
if(arraySize > 0)
delete [] aptr;
}
int size()
{
return arraySize;
}
T &operator[] (const int &sub)
{
if(sub < 0 || sub > arraySize)
{
subError();
}
return aptr[sub];
}
void pushBack( T val)
{
bptr = new T[arraySize+1];
for(int count=0; count<arraySize; count++)
{
*(bptr + count) = *(aptr + count);
}
*(bptr + arraySize)=val;
delete[] aptr;
aptr=bptr;
arraySize++;
}
void popBack()
{
bptr = new T[arraySize-1];
for(int count=0; count<arraySize-1; count++)
{
*(bptr + count) = *(aptr + count);
}
delete[] aptr;
aptr=bptr;
arraySize--;
}
int getArraySize()
{
return arraySize;
}
};
void Clear_Screen(void);
void Flush(void);
int main()
{
int num =0;
SimpleVector<int> vect;
cout << "Initialize vector's first element to 0\n" << endl;
vect.pushBack(0);
cout << " Displaying vector's content: ";
for(int i=0; i<vect.getArraySize(); i++)
{
cout<<vect[i]<<endl;
}
cout << endl;
system ("pause");
Clear_Screen();
cout<<"Enter a number to add as the vector's second element\n"<<endl;
cin >> num;
vect.pushBack(num);
cout << "Displaying vector's content: ";
for(int i=0; i<vect.getArraySize(); i++)
{
cout << vect[i] << " ";
}
cout << endl;
system ("pause");
Clear_Screen();
cout<<"Enter a number to add as the vector's third element\n"<<endl;
cin >> num;
vect.pushBack(num);
cout << "Displaying vector's content: ";
for(int i=0; i<vect.getArraySize(); i++)
{
cout << vect[i] << " ";
}
cout << endl;
system ("pause");
Clear_Screen();
cout<<"Remove last vector's element\n" << endl;
vect.popBack();
cout << "Displaying vector's content: ";
for(int i=0; i<vect.getArraySize(); i++)
{
cout << vect[i] << " ";
}
cout << endl;
system ("pause");
Clear_Screen();
cout<<"Remove the second vector's element\n"<<endl;
vect.popBack();
cout << "Displaying vector's content: ";
for(int i = 0; i < vect.getArraySize(); i++)
{
cout << vect[i] << " ";
}
cout << endl;
system ("pause");
Clear_Screen();
cout<<"Resize vector to 10 elements and initialize them to 888\n"<<endl;
SimpleVector<int> temp(10);
cout << "Displaying vector's content: ";
for(int i = 0; i < 10; i++)
{
temp[i]=888;
}
for(int i=0; i < 10; i++)
{
cout << " " << temp[i] << " ";
}
cout << endl;
system ("pause");
Clear_Screen();
cout<<"Enter a number to add to the back of the vector\n"<<endl;
cin >> num;
temp.pushBack(num);
cout << "Displaying vector's content: ";
for(int i=0; i<temp.getArraySize(); i++)
{
cout << " " << temp[i] << " ";
}
cout << endl;
system ("pause");
Clear_Screen();
cout << "\n\n Thank you for using the program!" << endl;
Flush();
cout << "\n\n Press any key to exit" << endl;
getchar();
return 0;
}
void Clear_Screen(void)
{
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void Flush(void)
{
int ch;
do
{
ch = getchar();
}
while (ch != EOF && ch != '\n');
clearerr(stdin);
}