1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#include <iostream>
#include <string>
template <typename T>
class FixedVector {
private:
size_t size_; // number of elements in the data structure
const size_t capacity_; // length of the array
T* array_; // pointer to dynamically allocated array
public:
// Constructors
FixedVector(size_t arraysize = 0); // Also serves as default constructor
FixedVector(const FixedVector& input ); // Copy constructor
~FixedVector();
// Getters / Setters
T& at(size_t index);
T& operator[](size_t index);
void push_back(const T& value);
void set(size_t index, const T& value);
void erase(size_t index);
size_t find(const T& value);
size_t insert(size_t beforeIndex, const T& value);
size_t size();
bool empty();
void clear();
// Overloaded Operators
FixedVector& operator= (const FixedVector& rhs); //Copy assignment
bool operator== (const FixedVector& rhs);
};
// Function member to look for value in FixedVector
// If value is in the FixedVector, then return the index of FixedVector that contains
// the value. If size_ is 0 (array is empty) or the value is not in FixedVector, then
// return size_
template <typename T>
size_t FixedVector<T>::find(const T& value) {
if(this->empty()){
return size_;
}
else{
for(auto i = 0; i<this->size();i++){
if(value == this->at(i)){
return i;
}
}
}
return size_;
}
// Function member to insert value in the FixedVector at index beforeIndex and return
// beforeIndex
// If beforeIndex is between 0 and size_, then insert the value by pushing all the
// elements to the right of beforeIndex one position to the right, and increment size_
// If size would exceed capacity, then exit with an error
// If beforeIndex is >=size_ then display error and do not do any changes to FixedVector
template <typename T>
size_t FixedVector<T>::insert(size_t beforeIndex, const T& value) {
if(beforeIndex>=size_){
std::cout<<"Error: Attempted to insert at an index greater or equal to the size of the vector.\n";
}
else if (beforeIndex<=size_ && beforeIndex>=0){
if(size_ + 1 <= capacity_){
size_++;
for(auto i =size_ - 1; i > beforeIndex; i--){
this->at(i+1) = this->at(i);
}
this->at(beforeIndex) = value;
}
else{
std::cout<<"Error: The size required to insert exceeds the capacity of the vector.\n";
}
}
return beforeIndex;
}
// Function member to test the equality between two FixedVectors
// It returns true if the two FixedVectors are exactly the same, false otherwise
template <typename T>
bool FixedArray<T>::operator== (const FixedVector& rhs){
bool tf;
for(auto i = 0; i<size_;i++){
if(this->at(i)!=rhs->at(i)){
tf = false;
break;
}
}
return tf;
}
int main() {
// testing the new implementation of a FixedVector
// declare & initialize a FixedVector of int with 10 elements
FixedVector<int> Array1(5);
// place 1,5,10 in the array
std::cout << "FixedArray gets the elements 1, 5, 10" << std::endl;
Array1.push_back(1);
Array1.push_back(5);
Array1.push_back(10);
// Try the find operation
std::cout << "Value 5 is at index " << Array1.find(5) << std::endl;
// Try the insert operation
std::cout << "Value 2 is inserted at index" << Array1.insert(1, 2) << std::endl;
// Try the == operator
FixedVector<int> Array2(5);
Array2.push_back(1);
Array2.push_back(5);
Array2.push_back(10);
if (Array1 == Array2)
std::cout << "The two arrays are the same." << std::endl;
else
std::cout << "The two arrays are different." << std::endl;
return 0;
}
|