Write a class that implements a vector of integers with the following template:
Class IntVector{ private:
// all member variables public:
IntVector( int n); // create a vector with size n
~IntVector(); // releases any memory allocated by the vector
Int getElement(int i); //returns the element i in the vector;
void setElement( int i, int v); // sets the element at location i to v
int getSize(); // returns the size of the vector;
void pushBack(int v); // adds the element v at the end of the array. The size of the array increases by 1.
bool popBack(); //removes the last element of the vector if any, and returns true, otherwise returns false }
-
- -
All methods, constructors and destructor prototypes described in the above class template have to be implemented
You can add any member variable you need to implement the class, but it has to be private.
You can add any extra method you might need to implement your class.
#include <iostream>
#include<vector>
using namespace std;
class IntVector
{
private:
vector<int> v;
public:
IntVector(int n)
{
v.size = n;
}
~IntVector()
{
v.clear();
}
int getElement(int i)
{
return v[i];
}
void setElement(int i, int e)
{
std::vector<int>::iterator it = v.begin() + i;
v.insert(it, e);
}
int getSize()
{
return v.size();
}
void pushBack(int e)
{
v.push_back(e);
}
bool popBack()
{
int s = v.size();
v.pop_back();
int s2 = v.size();
if (s > s2)
return true;
else
return false;
}
};
I think there is something wrong but I don't know what could anyone help me!!