Standard C++ Library Class Reference
six permutations, which, in order from first to last are: 1 2 3 , 1 3 2, 2 1 3, 2 3 1, 3 1 2, and 3 2 1.
The next_permutation algorithm takes a sequence defined by the range [first, last) and transforms
it into its next permutation, if possible. If such a permutation does exist, the algorithm completes
the transformation and returns true. If the permutation does not exist, next_permutation returns
false, and transforms the permutation into its "first" permutation (according to the lexicographical
ordering defined by either operator<, the default used in the first version of the algorithm,or comp,
which is user-supplied in the second version of the algorithm.)
For example, if the sequence defined by [first, last) contains the integers 3 2 1 (in that order), there
is not a "next permutation." Therefore, the algorithm transforms the sequence into its first
permutation (1 2 3) and returns false.
Complexity
At most (last - first)/2 swaps are performed.
Example
//
// permute.cpp
//
#include <numeric> //for accumulate
#include <vector> //for vector
#include <functional> //for less
#include <iostream.h>
int main()
{
//Initialize a vector using an array of ints
int a1[] = {0,0,0,0,1,0,0,0,0,0};
char a2[] = "abcdefghji";
//Create the initial set and copies for permuting
vector<int> m1(a1, a1+10);
vector<int> prev_m1((size_t)10), next_m1((size_t)10);
vector<char> m2(a2, a2+10);
vector<char> prev_m2((size_t)10), next_m2((size_t)10);
copy(m1.begin(), m1.end(), prev_m1.begin());
copy(m1.begin(), m1.end(), next_m1.begin());
copy(m2.begin(), m2.end(), prev_m2.begin());
copy(m2.begin(), m2.end(), next_m2.begin());
//Create permutations
prev_permutation(prev_m1.begin(),
prev_m1.end(),less<int>());
next_permutation(next_m1.begin(),
next_m1.end(),less<int>());
prev_permutation(prev_m2.begin(),