//Default constructor
template<typename T>
UniqueVector<T>::UniqueVector() {
//Didn't use initializer syntax because caused warnings
arrayCapacity = 3;
arraySize = 0;
array = new T[arrayCapacity];
}
template<typename T>
void UniqueVector<T>::doubleCapacity(){
//Create temp array with double the capacity
//delete array in heap and points array to temp
arrayCapacity*=2;
T* temp = new T[arrayCapacity];
for(unsignedint index=0; index<arraySize; index++){
temp[index] = array[index];
}
delete [] array;
array = temp;
}
//Returns capacity of the array
template<typename T>
unsignedint UniqueVector<T>::capacity(){
return arrayCapacity;
}
//returns size of the array
template<typename T>
unsignedint UniqueVector<T>::size() const{
return arraySize;
}
//Checks if size is 0
template<typename T>
bool UniqueVector<T>::empty(){
if(arraySize==0){
returntrue;
}
returnfalse;
}
template<typename T>
bool UniqueVector<T>::contains(const T& data){
//loops through array to check if the index has element data
for(unsignedint index=0; index < arraySize; index++){
if(array[index] == data){
returntrue;
}
}
returnfalse;
}
template<typename T>
bool UniqueVector<T>::at(unsignedint pos, T& data){
//If position is less than the size of the array
//stores the element of the array position into data
if(pos < arraySize){
data = array[pos];
returntrue;
}
returnfalse;
}
template<typename T>
bool UniqueVector<T>::insert(const T& data){
//insert data to the first pos
if(arraySize==0){
arraySize++;
array[0] = data;
returntrue;
}
//return false if index contains data
for(unsignedint index=0; index < arraySize; index++){
if(array[index] == data){
returnfalse;
}
}
//if size of array is equal to the capacity size, double capacity size
if(arraySize == arrayCapacity){
doubleCapacity();
}
//The last index of the array now contains data
array[arraySize] = data;
arraySize++;
returntrue;
}
template<typename T>
bool UniqueVector<T>::insert(const T& data, unsignedint pos){
//Checks if the position is valid
if(pos>arraySize){
returnfalse;
}
//insert data into the 1st position
if(arraySize==0){
arraySize++;
array[0] = data;
returntrue;
}
//return false if data is in array
for(unsignedint index=0; index < arraySize; index++){
if(array[index] == data){
returnfalse;
}
}
//if size of array is equal to the capacity size, double capacity size
if(arraySize == arrayCapacity)
doubleCapacity();
//Shifts the element of the index right by 1
arraySize++;
for(unsignedint index=arraySize-1; index>pos; index=index-1){
array[index] = array[index-1];
}
array[pos] = data;
returntrue;
}
template<typename T>
bool UniqueVector<T>::remove(const T& data){
//Makes sure the array size is not 0
if(arraySize==0){
returnfalse;
}
if(arraySize==1){
arraySize--;
returntrue;
}
//check if the element of index is the same as data, then shift
//elements starting from index to are shifted left by 1 and decrease size
for(unsignedint index=0; index < arraySize; index++){
if(array[index] == data){
//Shifts element of the array to the right
for(unsignedint index2=index; index2<arraySize-1; index2++)
array[index2] = array[index2+1];
arraySize--;
returntrue;
}
}
returnfalse;
}
template<typename T>
bool UniqueVector<T>::remove(unsignedint pos, T& data){
//if position is greater than size return false
if(pos>arraySize){
returnfalse;
}
//If size of array is 0 return false
if(arraySize==0){
returnfalse;
}
if(arraySize==1){
arraySize--;
returntrue;
}
//the element in position is stored in data
data = array[pos];
//overwrite element of the array by shifting by 1,
// where position is the first index to be overwritten
for(unsignedint index=pos; index<arraySize-1; index++){
array[index] = array[index+1];
}
arraySize--;
returntrue;
}
template<typename T2>
bool UniqueVector<T2>::operator==( const UniqueVector<T2> &rhs) const{
//If the array size is equivalent to the other vector's array size
//return true
if(arraySize == rhs.size()){
returntrue;
}
returnfalse;
}
#endif
I can't seem to figure what's wrong. I'm guessing it's classRoster.at(index,StudentName). The index is only reading the first and the last names of the vector.