#ifndef HEADER_VECTOR #define HEADER_VECTOR #include #include template class Vector{ public: Vector(); Vector(size_t capacity); Vector(size_t capacity, T value); ~Vector(); size_t Size(); size_t Capacity(); void PushBack(T value); template friend std::ostream& operator<<(std::ostream& stream, const Vector& vec); template friend std::istream& operator>>(std::istream& stream, Vector& vec); private: T* _values; size_t _capacity; size_t _size; }; template Vector::Vector() : _capacity(1) , _size(0) { // A vector must at least have a capacity of 1 _values = new T[1]; } template Vector::Vector(size_t capacity) : _capacity(capacity) , _size(0) { // Allocating the heap with the given capacity _values = new T[capacity]; } template Vector::Vector(size_t capacity, T value) : _capacity(capacity) , _size(capacity) { // Allocating the heap and setting the value to each element. _values = new T[capacity]; for(size_t i = 0; i Vector::~Vector() { delete[] _values; } template size_t Vector::Capacity() { return _capacity; } template size_t Vector::Size() { return _size; } template void Vector::PushBack(T value) { // if the next size will overload the heap if (_capacity < _size+1) { // Keep a track of the old pointer T* oldPtr = _values; // Creating a new pointer by resizing its capacity by factor 2. _values = new T[_size*2]; _capacity*=2; // Copying the old array onto the new for(size_t i=0; i<_size; i++) _values[i] = oldPtr[i]; // Deallocating the old array delete[] oldPtr; } // Setting the given value and incrementing the size _values[_size] = value; _size++; } // methode get template std::ostream& operator<<(std::ostream& stream, const Vector& vec) { for(size_t i=0; i std::istream& operator>>(std::istream& stream, Vector& vec) { for(size_t i=0; i> vec._values[i]; vec._size = vec._capacity; return stream; } #endif //HEADER_VECTOR