std::prev_permutation (3) - Linux Manuals
std::prev_permutation: std::prev_permutation
NAME
std::prev_permutation - std::prev_permutation
Synopsis
Defined in header <algorithm>
template< class BidirIt > (until C++20)
bool prev_permutation( BidirIt first, BidirIt last);
template< class BidirIt > (since C++20)
constexpr bool prev_permutation( BidirIt first, BidirIt last); (1)
template< class BidirIt, class Compare > (until C++20)
bool prev_permutation( BidirIt first, BidirIt last, Compare comp); (2)
template< class BidirIt, class Compare > (since C++20)
constexpr bool prev_permutation( BidirIt first, BidirIt last, Compare comp);
Transforms the range [first, last) into the previous 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 last permutation (as if by std::sort(first, last); std::reverse(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 precedes the old in lexicographical order. false if the first permutation was reached and the range was reset to the last permutation.
Exceptions
Any exceptions thrown from iterator operations or the element swap.
Complexity
At most (last-first)/2 swaps. Averaged over the entire sequence of permutations, typical implementations use about 3 comparisons and 1.5 swaps per call.
Possible implementation