#include #include #include using namespace std; struct person { string name, phone; person(string n, string p) { name = n; phone = p; } }; class vec { protected: person * * data; int capacity; int number; public: vec(int sz = 0) { data = new person * [sz]; capacity = sz; number = 0; } void push_back(person * p) { if (number == capacity) { int newcap = capacity * 2; if (newcap <= capacity) newcap = capacity + 1; grow_to(newcap); } data[number] = p; number += 1; } person * pop_back() { assert(number > 0); number -= 1; return data[number]; } void grow_to(int newcap) { if (newcap <= capacity) return; person * * newdata = new person * [newcap]; for (int i = 0; i < number; i += 1) newdata[i] = data[i]; delete [] data; data = newdata; capacity = newcap; } void shrink_to(int newcap) { if (newcap >= capacity) return; person * * newdata = new person * [newcap]; for (int i = 0; i < min(number, newcap); i += 1) newdata[i] = data[i]; delete [] data; data = newdata; capacity = newcap; } }; int main() { vec V; V.push_back(new person("J Smith", "1234567890")); V.push_back(new person("F Jones", "9876543210")); V.push_back(new person("P Ratbag", "1111111111")); person * x; x = V.pop_back(); cout << x->name << " " << x -> phone << "\n"; x = V.pop_back(); cout << x->name << " " << x -> phone << "\n"; x = V.pop_back(); cout << x->name << " " << x -> phone << "\n"; }