#include <iostream>
using namespace std;
template<typename T> class myvector{
T *arr;
int capacity;
int current;
public:
myvector()
{
arr= new T[1];
capacity = 1;
current =0;
}
void push(int data)
{
if(capacity == current)
{
capacity = 2*current; //double the capacity
T *temp = new T[capacity];// create a new set of integers with that capacity
for(int i=0;i<current;i++)
{
temp[i] = arr[i];
}
delete arr;
arr=temp;
}
arr[current]=data;
current++;
}
void push(int index, int data)
{
if(index == capacity)
push(data);
else
arr[index] = data;
}
void pop()
{
current --; // delete the last
}
int top()
{
return arr[current];
}
int getsize()
{
return current;
}
int getcapacity()
{
return capacity;
}
int get(int index)
{
if(index>current)
return -1;
return arr[index];
}
void print()
{
for (int i = 0; i < current; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
};
int main() {
myvector<int>v;
v.push(20);
v.push(10);
v.print();
v.push(30);
v.push(40);
v.push(50);
v.print();
v.pop();
v.print();
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: