Skip to content

Commit

Permalink
added vector class
Browse files Browse the repository at this point in the history
  • Loading branch information
codesanta142 committed Oct 5, 2021
1 parent f90f9a4 commit 65c8051
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <bits/stdc++.h>
using namespace std;


#define ll long long int
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define fo(i,a,b) for(int i=a ; i<b ; i++)
#define foe(i,a,b) for(int i=a ; i<=b ; i++)
#define endl '\n'
#define ff first
#define ss second
#define MOD 1000000007
template <typename P> class vectorClass
{
P* a;
int size,current;

public:
vectorClass()
{
a = new P[1];
size = 1;
current = 0;
}
void push(P data)
{
if (current == size) {
P* temp = new P[2 * size];

for (int i = 0; i <size; i++) {
temp[i] = a[i];
}

delete[] a;
size *= 2;
a = temp;
}

a[current] = data;
current++;
}

void push(int data, int index)
{
if (index == size)
push(data);
else
a[index] = data;
}


P get(int index)
{
if (index < current)
return a[index];
}


void pop() { current--; }
int vecsize() { return current; }
int getcapacity() { return size; }

void print()
{
for (int i = 0; i < current; i++) {
cout << a[i] << " ";
}
cout << endl;
}
};


int main()
{
vectorClass<int> v;
v.push(1);
v.push(2);
v.push(3);
v.push(4);
v.push(5);

cout << "Vector size : " << v.vecsize() << endl;
cout << "Vector capacity : " << v.getcapacity() << endl;
cout << "Vector elements : ";
v.print();
v.pop();
cout << "Vector elements after one pop: ";
v.print();


return 0;
}




0 comments on commit 65c8051

Please sign in to comment.