Name
next_permutation function template — Generates next permutation
Synopsis
template<typename BidiIter> bool next_permutation(BidiIter first, BidiIter last); template<typename BidiIter, typename Compare> bool next_permutation(BidiIter first, BidiIter last, Compare comp);
The next_permutation
function template rearranges the contents of the range [first
, last
) for the next permutation, assuming
that there is a set of lexicographically ordered permutations. The
return value is true
if the next
permutation is generated, or false
if the range is at the last
permutation, in which case the function cycles, and the first
permutation is generated (that is, with all elements in ascending
order).
Figure 13-9 shows
all the permutations, in order, for a sequence. The next_permutation
function swaps elements
to form the next permutation. For example, if the input is
permutation 2, the result is permutation 3. If the input is
permutation 6, the result is permutation 1, and next_permutation
returns false
.
Example
Example 13-4 shows
a simple program that prints all the permutations of a sequence of
integers. You can use this program to better understand the next_permutation
function template.
#include <algorithm> #include <iostream> #include <istream> #include <iterator> #include <ostream> #include <vector> void print(const std::vector<int>& ...
Get C++ In a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.