[alg.permutation.generators] # 26 Algorithms library [[algorithms]](./#algorithms) ## 26.8 Sorting and related operations [[alg.sorting]](alg.sorting#alg.permutation.generators) ### 26.8.13 Permutation generators [alg.permutation.generators] [🔗](#lib:next_permutation) `template constexpr bool next_permutation(BidirectionalIterator first, BidirectionalIterator last); template constexpr bool next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp); template<[bidirectional_iterator](iterator.concept.bidir#concept:bidirectional_iterator "24.3.4.12 Concept bidirectional_­iterator [iterator.concept.bidir]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]") S, class Comp = ranges::less, class Proj = identity> requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8 Concept sortable [alg.req.sortable]") constexpr ranges::next_permutation_result ranges::next_permutation(I first, S last, Comp comp = {}, Proj proj = {}); template<[bidirectional_range](range.refinements#concept:bidirectional_range "25.4.6 Other range refinements [range.refinements]") R, class Comp = ranges::less, class Proj = identity> requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8 Concept sortable [alg.req.sortable]"), Comp, Proj> constexpr ranges::next_permutation_result> ranges::next_permutation(R&& r, Comp comp = {}, Proj proj = {}); ` [1](#1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L11918) Let comp be less{} and proj be identity{} for overloads with no parameters by those names[.](#1.sentence-1) [2](#2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L11923) *Preconditions*: For the overloads in namespace std,BidirectionalIterator meets the [*Cpp17ValueSwappable*](swappable.requirements#:Cpp17ValueSwappable "16.4.4.3 Swappable requirements [swappable.requirements]") requirements ([[swappable.requirements]](swappable.requirements "16.4.4.3 Swappable requirements"))[.](#2.sentence-1) [3](#3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L11929) *Effects*: Takes a sequence defined by the range [first, last) and transforms it into the next permutation[.](#3.sentence-1) The next permutation is found by assuming that the set of all permutations is lexicographically sorted with respect to comp and proj[.](#3.sentence-2) If no such permutation exists, transforms the sequence into the first permutation; that is, the ascendingly-sorted one[.](#3.sentence-3) [4](#4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L11939) *Returns*: Let B be true if a next permutation was found and otherwise false[.](#4.sentence-1) Returns: - [(4.1)](#4.1) B for the overloads in namespace std[.](#4.1.sentence-1) - [(4.2)](#4.2) { last, B } for the overloads in namespace ranges[.](#4.2.sentence-1) [5](#5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L11949) *Complexity*: At most (last - first) / 2 swaps[.](#5.sentence-1) [🔗](#lib:prev_permutation) `template constexpr bool prev_permutation(BidirectionalIterator first, BidirectionalIterator last); template constexpr bool prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp); template<[bidirectional_iterator](iterator.concept.bidir#concept:bidirectional_iterator "24.3.4.12 Concept bidirectional_­iterator [iterator.concept.bidir]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]") S, class Comp = ranges::less, class Proj = identity> requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8 Concept sortable [alg.req.sortable]") constexpr ranges::prev_permutation_result ranges::prev_permutation(I first, S last, Comp comp = {}, Proj proj = {}); template<[bidirectional_range](range.refinements#concept:bidirectional_range "25.4.6 Other range refinements [range.refinements]") R, class Comp = ranges::less, class Proj = identity> requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8 Concept sortable [alg.req.sortable]"), Comp, Proj> constexpr ranges::prev_permutation_result> ranges::prev_permutation(R&& r, Comp comp = {}, Proj proj = {}); ` [6](#6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L11977) Let comp be less{} and proj be identity{} for overloads with no parameters by those names[.](#6.sentence-1) [7](#7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L11982) *Preconditions*: For the overloads in namespace std,BidirectionalIterator meets the [*Cpp17ValueSwappable*](swappable.requirements#:Cpp17ValueSwappable "16.4.4.3 Swappable requirements [swappable.requirements]") requirements ([[swappable.requirements]](swappable.requirements "16.4.4.3 Swappable requirements"))[.](#7.sentence-1) [8](#8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L11988) *Effects*: Takes a sequence defined by the range [first, last) and transforms it into the previous permutation[.](#8.sentence-1) The previous permutation is found by assuming that the set of all permutations is lexicographically sorted with respect to comp and proj[.](#8.sentence-2) If no such permutation exists, transforms the sequence into the last permutation; that is, the descendingly-sorted one[.](#8.sentence-3) [9](#9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L11998) *Returns*: Let B be true if a previous permutation was found and otherwise false[.](#9.sentence-1) Returns: - [(9.1)](#9.1) B for the overloads in namespace std[.](#9.1.sentence-1) - [(9.2)](#9.2) { last, B } for the overloads in namespace ranges[.](#9.2.sentence-1) [10](#10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L12008) *Complexity*: At most (last - first) / 2 swaps[.](#10.sentence-1)