1: //deque05.cpp 3: #include <iostream> 4: #include <string> 5: #include <deque> 6: using namespace std; 9: int main() 10: { 11: cout << "\nThis program illustrates typical uses of the " 12: "default deque class iterator\n(which is a random " 13: "access iterator), and shows additional uses of member\n" 14: "functions begin() and end(), as well as the use of " 15: "operators =, +, -, ++,\n--, != and * with deque class " 16: "iterators."; 17: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 19: deque<int> d1(8); 21: cout << "\nFirst we enter some integer squares into " 22: "a deque of size 8, and then display: \n"; 23: //Create and initialize an iterator. 24: deque<int>::iterator p = d1.begin(); 25: int i = 0; 26: while(p != d1.end()) 27: { 28: *p = i*i; //Assign i*i to deque d via iterator p. 29: p++; //Advance the iterator **after** using it. 30: i++; //Increment the value to be squared. 31: } 32: p = d1.begin(); 33: while(p != d1.end()) cout << *p++ << " "; //Note *p++ "idiom". 34: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 36: cout << "\nNext we double the values in the deque and " 37: "re-display (backwards this time): \n"; 38: p = d1.begin(); 39: while(p != d1.end()) 40: { 41: *p = *p * 2; 42: p++; 43: } 44: //This time, initialize the iterator to d1.end(). 45: //And decrement the iterator **before** using it! 46: p = d1.end(); 47: while(p != d1.begin()) cout << *--p << " "; //Note *--p "idiom". 48: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 50: cout << "\nFinally, we display (using iterators) some " 51: "particular deque values:"; 52: cout << "\nFirst component of the deque = " << *(d1.begin()); 53: cout << "\nThird component of the deque = " << *(d1.begin()+2); 54: cout << "\nLast component of the deque = " << *(d1.end()-1); 55: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 56: }