25 Algorithms library [algorithms]

25.7 Sorting and related operations [alg.sorting]

25.7.12 Permutation generators [alg.permutation.generators]

template<class BidirectionalIterator> constexpr bool next_permutation(BidirectionalIterator first, BidirectionalIterator last); template<class BidirectionalIterator, class Compare> constexpr bool next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp); template<BidirectionalIterator I, Sentinel<I> S, class Comp = ranges::less, class Proj = identity> requires Sortable<I, Comp, Proj> constexpr bool ranges::next_permutation(I first, S last, Comp comp = {}, Proj proj = {}); template<BidirectionalRange R, class Comp = ranges::less, class Proj = identity> requires Sortable<iterator_t<R>, Comp, Proj> constexpr bool ranges::next_permutation(R&& r, Comp comp = {}, Proj proj = {});
Let comp be less{} and proj be identity{} for overloads with no parameters by those names.
Requires: For the overloads in namespace std, BidirectionalIterator shall meet the Cpp17ValueSwappable requirements ([swappable.requirements]).
Effects: Takes a sequence defined by the range [first, last) and transforms it into the next permutation.
The next permutation is found by assuming that the set of all permutations is lexicographically sorted with respect to comp and proj.
If no such permutation exists, transforms the sequence into the first permutation; that is, the ascendingly-sorted one.
Returns: true if and only if a next permutation was found.
Complexity: At most (last - first) / 2 swaps.
template<class BidirectionalIterator> constexpr bool prev_permutation(BidirectionalIterator first, BidirectionalIterator last); template<class BidirectionalIterator, class Compare> constexpr bool prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp); template<BidirectionalIterator I, Sentinel<I> S, class Comp = ranges::less, class Proj = identity> requires Sortable<I, Comp, Proj> constexpr bool ranges::prev_permutation(I first, S last, Comp comp = {}, Proj proj = {}); template<BidirectionalRange R, class Comp = ranges::less, class Proj = identity> requires Sortable<iterator_t<R>, Comp, Proj> constexpr bool ranges::prev_permutation(R&& r, Comp comp = {}, Proj proj = {});
Let comp be less{} and proj be identity{} for overloads with no parameters by those names.
Requires: For the overloads in namespace std, BidirectionalIterator shall meet the Cpp17ValueSwappable requirements ([swappable.requirements]).
Effects: Takes a sequence defined by the range [first, last) and transforms it into the previous permutation.
The previous permutation is found by assuming that the set of all permutations is lexicographically sorted with respect to comp and proj.
If no such permutation exists, transforms the sequence into the last permutation; that is, the descendingly-sorted one.
Returns: true if and only if a previous permutation was found.
Complexity: At most (last - first) / 2 swaps.