1: //prev_permutation1a.cpp
3: #include <iostream>
4: #include <vector>
5: #include <algorithm>
6: #include <iomanip>
7: using namespace std;
9: int main()
10: {
11: cout << "\nThis program illustrates the use of the STL "
12: "prev_permutation() algorithm\n(default version) to generate "
13: "all permutations of a vector of integers,\nin decreasing order, "
14: "and also to demonstrate what happens to the return\nvalue of "
15: "the algorithm when the \"end\" of a permutation sequence is "
16: "reached\nand we then \"roll over\" to begin a new sequence.";
17: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
19: cout << "\nFirst, we declare a vector of 3 integers and show all of "
20: "its permuations.";
21: int a1[] = {3, 2, 1};
22: vector<int> v1(a1, a1+3);
23: cout << "\nHere are the contents of the vector before any "
24: "permutations are generated:\n";
25: for (vector<int>::size_type i=0; i<v1.size(); i++)
26: cout << v1.at(i) << " ";
27: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
29: cout << "\nAnd here are the contents of the vector as each "
30: "permutation is generated:\n";
31: while (prev_permutation(v1.begin(), v1.end()))
32: {
33: for (vector<int>::size_type i=0; i<v1.size(); i++)
34: cout << v1.at(i) << " ";
35: cout << endl;
36: }
37: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
39: cout << "\nFinally (for this vector), here are its contents"
40: "\nafter all permutations have been generated:\n";
41: for (vector<int>::size_type i=0; i<v1.size(); i++)
42: cout << v1.at(i) << " ";
43: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
45: int a2[] = {5, 3, 2, 4, 1};
46: vector<int> v2(a2, a2+5);
47: cout << "\nHere are the contents of a second vector of 5 integers "
48: "\nbefore any permutations are generated:\n";
49: for (vector<int>::size_type i=0; i<v2.size(); i++)
50: cout << v2.at(i) << " ";
51: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
53: cout << "\nNow we show the return value of the algorithm, and the "
54: "contents of\nthis vector, after each of 6 permutations of just "
55: "the middle 3 values.";
56: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
57: for (int i=1; i<=6; i++)
58: {
59: cout << "Return value: " << boolalpha
60: << next_permutation(v2.begin()+1, v2.end()-1) << "\t";
61: cout << "Vector contents: ";
62: for (vector<int>::size_type i=0; i<v2.size(); i++)
63: cout << v2.at(i) << " ";
64: cout << endl;
65: }
66: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
67: }