#ifndef myvector #define myvector #include #include template class MyVectorT { public: MyVectorT(int c=2):capacity{c}, data{std::make_unique(capacity)}{}; void push_back(T item) { std::unique_ptr tmp; if (size >= capacity) { tmp = std::move(data); capacity *= 2; data = std::make_unique(capacity); for(int i = 0; i < size; i++) { data[i] = tmp[i]; } } data[size] = item; size++; } MyVectorT(const MyVectorT & other) { Copy(other); } MyVectorT & operator =(const MyVectorT & other) { if (this != &other) { Copy(other); } return *this; } void pop_back(){ // could add code to shrink the vector if size < 1/2 capacity if (size > 0) { size--; } else { throw (std::out_of_range{"Can not pop_back an empty vector"}); } } T back() const { if (size > 0) { return data[size-1]; } else { throw (std::out_of_range{"Can not back an empty vector"}); } } // for right hand side const T & operator[](size_t s) const { return data[s]; } // for left hand side T & operator [] (size_t s) { return data[s]; } int Size() const { return size; } int Capacity() const { return capacity; } private: void Copy(const MyVectorT & src) { size= src.size; capacity = src.capacity; data = std::make_unique(capacity); for(int i =0; i < size; i++) { data[i] = src.data[i]; } } int capacity; int size {0}; std::unique_ptr data; }; #endif