#include #include "MyVectorT.h" #include #include using namespace std; template void PrintVector(const MyVectorT & v) { int i; cout << "\t"; for(i =0; i < v.Size(); i++) { cout << v[i] << " "; } cout << endl; return; } template void Sort(MyVectorT & v) { int i, j, small; T tmp; for(i= 0; i < v.Size(); i++) { small = i; for (j =i+1; j < v.Size(); j++) { if (v[small] > v[j]) { small= j; } } if (small != i) { swap(v[i], v[small]); } } } void DoubleVector(MyVectorT & v); void CopyConstructorTest(MyVectorT v); void IntVectorTest(); void StringVectorTest(); void ErrorTest(); int main() { IntVectorTest(); cout << endl << endl; StringVectorTest(); cout << endl << endl; ErrorTest(); return 0; } void ErrorTest() { MyVectorT data; cout << "In the error vector test" << endl; bool caught{false}; try { data.pop_back(); } catch (const out_of_range & e) { caught = true; } if (not caught) { cout << "Failed to catch pop_back of empty vector" << endl; } caught = false; try { data.back(); } catch (const out_of_range & e) { caught = true; } if (not caught) { cout << "Failed to catch back of empty vector" << endl; } cout << "Done with error vector test" << endl; } void StringVectorTest() { MyVectorTdata; cout << "In the string vector test" << endl; data.push_back("now"); data.push_back("is"); data.push_back("the"); data.push_back("time"); data.push_back("for"); data.push_back("all"); data.push_back("good"); data.push_back("people"); cout << "The original data " << endl; PrintVector(data); cout << "The sorted list " << endl; Sort(data); PrintVector(data); cout << endl; cout << "Testing pop_back" << endl; for (int i = data.Size(); i > 0; --i) { data.pop_back(); PrintVector(data); } cout << "Done with the string vector test " << endl; } void IntVectorTest() { MyVectorT data; MyVectorT data2; cout << "In the int vector test " << endl; cout << "Adding a 1" << endl; data.push_back(1); PrintVector(data); cout << endl; cout << "Adding a 3" << endl; data.push_back(3); PrintVector(data); cout << endl; cout << "Adding a 4 5 6 7 8" << endl; data.push_back(4); data.push_back(5); data.push_back(6); data.push_back(7); data.push_back(8); PrintVector(data); cout << endl; cout << "Making a copy, it is " << endl; data2 = data; PrintVector(data2); cout << endl; cout << "Doubling the first " << endl; DoubleVector(data); cout << "The first " << endl; PrintVector(data); cout << "The second (should be undoubled) " << endl; PrintVector(data2); cout << endl; cout << "Doing the Copy Constructor Test" << endl; CopyConstructorTest(data2); cout << "After the copy constructor test " << endl; PrintVector(data2); cout << "Done with the int vector test " << endl; } void DoubleVector(MyVectorT & v){ for(int i=0; i < v.Size(); i++) { v[i] *= 2; } } void CopyConstructorTest(MyVectorT v){ PrintVector(v); for(int i = 0; i < v.Size(); i++) { v[i] += 100; } PrintVector(v); }