Just to clarify this is part of my programming assignment I know how people hate when they ask about hw, but I'm stumped and my current understanding of the topic needs to be clarified.
I need to create a Class called UniqueVector, I have to create this using dynamic array(no vectors allowed). All the data in the array has to be unique ( no duplicates). Now initially they should all begin with size 3, but nothing is inside. I believe I have that correct however I might be wrong.
#include <iostream>
using namespace std;
template<typename T>
class UniqueVector {
public:
UniqueVector();
unsigned int capacity();//Returns the size of the space currently allocated for the vector.
unsigned int size(); //- Returns the current number of elements in the vector.
private:
T* uniVector;
};
template<typename T>
UniqueVector<T> :: UniqueVector(){
uniVector = new T[3]; // default constructor
delete []uniVector;
}
template<typename T>
unsigned int UniqueVector<T> :: capacity()
{
int uni_size= sizeof(uniVector)/sizeof(uniVector[0]);
cout << uni_size << endl; //Gives 1
return (3); //forcing return size 3 even tho it is wrong
}
template<typename T>
unsigned int UniqueVector<T> :: size()
{
int unique_size=0;
for(int i=0; i<3; i++){
cout <<i<<" "<< uniVector[i] << endl; //[0] and [1] gives values? But [2] doesnt as it should
if(uniVector[i]!=NULL){ //Only [2] is empty for some reason
unique_size++; // if arrays arent empty, adds to total
}
};
return (unique_size);
}
int main()
{
UniqueVector<int> testVector;
cout<< "The cap is " << testVector.capacity() << endl;
cout<< "The size is " <<testVector.size() << endl;
return 0;
}
At first my capacity function worked if it was just a T uniVector [3] in private and no default constructor, but now it just returns 1 when it should be 3.
Size() never worked in the first place, because I'm some how creating values when I never inputted anything but the size.