#include "vector.h" #include "MenuDriver.h" #include #include using namespace std; using namespace KW; class Driver : public MenuDriver { public: Driver(int argc, const char* argv[]) : MenuDriver(argc,argv) { stringstream menu; menu << "(At) at(i)" << endl; menu << "(As) at(i) = val" << endl; menu << "(Ci) contains(val)" << endl; menu << "(Cu) count(val)" << endl; menu << "(Em) empty()" << endl; menu << "(Er) erase(i)" << endl; menu << "(Ix) index(val)" << endl; menu << "(In) insert(i,val)" << endl; menu << "(N) new (empty) vector" << endl; menu << "(O) output" << endl; menu << "(Po) pop_back()" << endl; menu << "(Pu) push_back(val)" << endl; menu << "(Rm) remove(val)" << endl; menu << "(Rv) reverse()" << endl; menu << "(Sz) size()" << endl; menu << "(Q) Quit" << endl; MENU = menu.str(); } protected: vector v; size_t getIndex() { return read("Enter an index: "); } string getValue(const string& prompt = "Enter a string: ") { return readLine(prompt); } bool handleChoice(const string& choice) { bool goOn = true; if ((choice == "At") || (choice == "at")) { // at size_t i = getIndex(); string val; try { string &item = v.at(i); cout << "at("< temp; v = temp; cout << "Vector has been reinitialized to empty." << endl; } else if ((choice == "O") || (choice == "o")) { // output try { cout << v << endl; } catch (...) { cout << "output operator (<<) cause unexpected exception" << endl; } } else if ((choice == "Po") || (choice == "po")) { // pop_back() try { if (!v.empty()) { try { v.pop_back(); cout << "pop_back() completed." << endl; } catch (...) { cout << "pop_back() threw unexpected exception" << endl; } } else { cout << "Our driver does not allow pop_back on an empty vector." << endl << "(note: STL does not actually perform such error checking itself)" << endl; } } catch (...) { cout << "Before calling pop_back, we testedcalled empty(). " << endl << "that call threw unexpected exception" << endl; } } else if ((choice == "Pu") || (choice == "pu")) { // push_back(val) string val = getValue(); try { v.push_back(val); cout << "push_back("<<'"'<