std::next_permutation (3) - Linux Manuals
std::next_permutation: std::next_permutation
NAME
std::next_permutation - std::next_permutation
Synopsis
Defined in header <algorithm>
template< class BidirIt > (until C++20)
bool next_permutation( BidirIt first, BidirIt last );
template< class BidirIt > (since C++20)
constexpr bool next_permutation( BidirIt first, BidirIt last ); (1)
template< class BidirIt, class Compare > (until C++20)
bool next_permutation( BidirIt first, BidirIt last, Compare comp ); (2)
template< class BidirIt, class Compare > (since C++20)
constexpr bool next_permutation( BidirIt first, BidirIt last, Compare comp );
Transforms the range [first, last) into the next permutation from the set of all permutations that are lexicographically ordered with respect to operator< or comp. Returns true if such permutation exists, otherwise transforms the range into the first permutation (as if by std::sort(first, last)) and returns false.
Parameters
first, last - the range of elements to permute
comp - While the signature does not need to have const &, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const) Type1 and Type2 regardless of value_category (thus, Type1 & is not allowed
Type requirements
-
BidirIt must meet the requirements of ValueSwappable and LegacyBidirectionalIterator.
Return value
true if the new permutation is lexicographically greater than the old. false if the last permutation was reached and the range was reset to the first permutation.
Exceptions
Any exceptions thrown from iterator operations or the element swap.
Complexity
At most N/2 swaps, where N = std::distance(first, last). Averaged over the entire sequence of permutations, typical implementations use about 3 comparisons and 1.5 swaps per call.
Possible implementation