std::next_permutation and prev_permutation in C++

std::next_permutation

It is used to rearrange the elements in the range [first, last) into the next lexicographically greater permutation. A permutation is each one of the N! possible arrangements the elements can take (where N is the number of elements in the range). Different permutations can be ordered according to how they compare lexicographically to each other.

Syntax:

template
bool next_permutation (BidirectionalIterator first,
BidirectionalIterator last);Parameters:
first, last : Bidirectional iterators to the initial
and final positions of the sequence. The range
used is [first, last), which contains all the elements
between first and last, including the element pointed
by first but not the element pointed by last.
return value: true : if the function could rearrange
the object as a lexicographicaly greater permutation.
Otherwise, the function returns false to indicate that
the arrangementis not greater than the previous,
but the lowest possible (sorted in ascending order).

Application : next_permutation is to find next lexicographicaly greater value for given array of values.
Examples:

It is used to rearranges the elements in the range [first, last) into the previous lexicographically-ordered permutation. A permutation is each one of the N! possible arrangements the elements can take (where N is the number of elements in the range). Different permutations can be ordered according to how they compare lexicographicaly to each other.

Syntax :

template
bool prev_permutation (BidirectionalIterator first,
BidirectionalIterator last );parameters:
first, last : Bidirectional iterators to the initial
and final positions of the sequence. The range
used is [first, last), which contains all the
elements between first and last, including
the element pointed by first but not the element
pointed by last.
return value: true : if the function could rearrange
the object as a lexicographicaly smaller permutation.
Otherwise, the function returns false to indicate that
the arrangement is not less than the previous,
but the largest possible (sorted in descending order).

Application : prev_permutation is to find previous lexicographicaly smaller value for given array of values.
Examples:

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.