#include #include using namespace std; template class vector { protected: T * data; int capacity; int numitems; public: vector(); ~vector(); void resize(int newsize); void push_back(T value); T back(); void pop_back(); T & at(int position); int size(); }; template vector::vector() { capacity = 0; numitems = 0; data = nullptr; } template vector::~vector() { delete [] data; } template void vector::resize(int newsize) { if (newsize < capacity) return; T * newdata = new T[newsize]; for (int i = 0; i < numitems; i += 1) newdata[i] = data[i]; delete [] data; data = newdata; capacity = newsize; } template void vector::push_back(T value) { if (numitems >= capacity) { int newsize = capacity * 2; if (newsize <= capacity) newsize = capacity + 1; resize(newsize); } data[numitems] = value; numitems += 1; } template T vector::back() { assert(numitems > 0); return data[numitems - 1]; } template void vector::pop_back() { assert(numitems > 0); numitems -= 1; } template T & vector::at(int position) { assert(position >= 0); assert(position < numitems); return data[position]; } template int vector::size() { return numitems; } int main() { vector V; vector SV; V.push_back(123); V.push_back(99); V.push_back(7); V.push_back(12'345'678); V.at(2) = 777777; VS.push_back("one"); VS.push_back("two"); VS.push_back("three"); VS.push_back("four"); VS.push_back("five"); VS.push_back("six"); VS.push_back("seven"); for (int i = 0; i < V.size(); i += 1) cout << V.at(i) << "\n"; for (int i = 0; i < VS.size(); i += 1) cout << VS.at(i) << "\n"; }