parciales/2021-1C-3p/vector.h (864B)
1 #ifndef VECTOR_H 2 #define VECTOR_H 3 #define VECTOR_STARTING_CAPACITY 5 4 5 template <typename Type> 6 class Vector { 7 private: 8 Type *data; 9 unsigned int size; 10 unsigned int alloc; 11 12 public: 13 Vector() { 14 this->data = new Type[VECTOR_STARTING_CAPACITY]; 15 this->alloc = VECTOR_STARTING_CAPACITY; 16 this->size = 0; 17 } 18 19 virtual ~Vector() { 20 delete[] this->data; 21 } 22 23 void append(Type value) { 24 if(this->size == this->alloc) { 25 this->alloc *= 2; 26 Type *newData = new Type[this->alloc]; 27 for(size_t i = 0; i < this->size; ++i) { 28 newData[i] = this->data[i]; 29 } 30 delete[] this->data; 31 this->data = newData; 32 } 33 this->data[this->size] = value; 34 this->size++; 35 } 36 37 unsigned int getSize() { 38 return this->size; 39 } 40 41 Type getAtIndex(unsigned int index) { 42 if(index > this->size) { 43 throw "index out of bounds"; 44 } 45 return this->data[index]; 46 } 47 }; 48 49 #endif
