// File: lab13.cpp // #include #include #include #include using namespace std; void add5 (int &n) { n += 5 ; } int main() { vector V ; list L ; vector::iterator it1 ; list::iterator it2 ; // Add these elements 60, 20, 40, 40, 30, 70, 90, 60, 10, 80, 80, 10 // to both the vector and the list. V.push_back(60); V.push_back(20); V.push_back(40); V.push_back(30); V.push_back(70); V.push_back(90); V.push_back(60); V.push_back(80); V.push_back(10); L.push_back(60); L.push_back(20); L.push_back(40); L.push_back(30); L.push_back(70); L.push_back(90); L.push_back(60); L.push_back(80); L.push_back(10); // Print elements of V that were just added using an iterator cout << "ORIGINAL VECTOR:\n" ; for (it1 = V.begin() ; it1 != V.end() ; it1++) { cout << *it1 << " " ; } cout << endl ; //---------------------------------------------------------------------------- // STEP 1 // Warm up exercise: repeat code and print out the contents of linked list L. // Expected Output 60 20 40 40 30 70 90 60 10 80 80 10 // // Expected output: 60 20 40 30 70 90 60 80 10 cout << "\n\nSTEP 1:\n" ; // ------------------------ // | ADD STEP 1 CODE HERE | // ------------------------ //---------------------------------------------------------------------------- // STEP 2 // Insert 50 just before the 70 in vector V using V.insert(it1, 50). // Hint: use a while loop to loop through V until you find the 70. // // Expected output: 60 20 40 30 50 70 90 60 80 10 // ------------------------ // | ADD STEP 2 CODE HERE | // ------------------------ // Print again cout << "\n\nSTEP 2:\n" ; for (it1 = V.begin() ; it1 != V.end() ; it1++) { cout << *it1 << " " ; } cout << endl ; //---------------------------------------------------------------------------- // STEP 3 // Remove 90 from linked list L. // Use: L.erase(it2) // // Expected output: 60 20 40 30 70 60 80 10 // ------------------------ // | ADD STEP 3 CODE HERE | // ------------------------ cout << "\n\nSTEP 3:\n" ; // Print L again. Cut and paste code from STEP 1: // ------------------------ // | ADD STEP 3 CODE HERE | // ------------------------ //---------------------------------------------------------------------------- // STEP 4 // Apply add5() to every element of V and L // Expected output: // 65 25 45 35 55 75 95 65 85 15 // 65 25 45 35 75 65 85 15 // ------------------------ // | ADD STEP 4 CODE HERE | // ------------------------ // Print out both V and L cout << "\n\nSTEP 4:\n" ; for (it1 = V.begin() ; it1 != V.end() ; it1++) { cout << *it1 << " " ; } cout << endl ; // Cut and paste print code to print L from above. // ------------------------ // | ADD STEP 4 CODE HERE | // ------------------------ //---------------------------------------------------------------------------- // STEP 5 // Vectors have random access iterators: // Expected Output: 70 30 50 40 60 75 95 65 85 15 // ------------------------ // | ADD STEP 5 CODE HERE | // ------------------------ cout << "\n\nSTEP 5:\n" ; for (it1 = V.begin() ; it1 != V.end() ; it1++) { cout << *it1 << " " ; } //---------------------------------------------------------------------------- // STEP 6 // STL has a generic sort algorithm // Expected output: 15 30 40 50 60 65 70 75 85 95 // ------------------------ // | ADD STEP 6 CODE HERE | // ------------------------ cout << "\n\nSTEP 6:\n" ; for (it1 = V.begin() ; it1 != V.end() ; it1++) { cout << *it1 << " " ; } //---------------------------------------------------------------------------- // STEP 7 // Try using sort() on L, you'll get a bunch of compile time errors. // ------------------------ // | ADD STEP 7 CODE HERE | // ------------------------ }