1: //advance.cpp 3: #include <iostream> 4: #include <vector> 5: #include <list> 6: #include <iterator> 7: #include <algorithm> 8: using namespace std; 10: int main() 11: { 12: cout << "\nThis program illustrates the use of the advance() " 13: "function\nto move an iterator a given number of positions."; 14: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 16: int a[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; 17: list<int> intList(a, a+10); 18: cout << "\nHere are the values in a list of integers:\n"; 19: copy(intList.begin(), intList.end(), ostream_iterator<int>(cout, " ")); 20: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 22: list<int>::iterator p = intList.begin(); 23: advance(p, 7); 24: cout << "\nThe 8th value in the list is " << *p << "."; 25: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 27: advance(p, -5); 28: cout << "\nThe 3rd value in the list is " << *p << "."; 29: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 31: list<int>::reverse_iterator p_r = intList.rbegin(); 32: advance(p_r, 5); 33: cout << "\nThe 6th value from the end of the list is " << *p_r << "."; 34: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 36: int b[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19}; 37: vector<int> v(b, b+10); 38: cout << "\nHere are the values in a vector of integers:\n"; 39: copy(v.begin(), v.end(), ostream_iterator<int>(cout, " ")); 40: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 42: vector<int>::iterator p_v = v.begin(); 43: advance(p_v, 6); 44: cout << "\nThe 7th value in the vector is " << *p_v << "."; 45: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 46: }