std::ranges::prev_permutation, std::ranges::prev_permutation_result
| Definiert in Header <algorithm> |
||
| Aufruf-Signatur |
||
| template< std::bidirectional_iterator I, std::sentinel_for<I> S, class Comp = ranges::less, class Proj = std::identity > |
(1) | (seit C++20) |
| template< ranges::bidirectional_range R, class Comp = ranges::less, class Proj = std::identity > |
(2) | (seit C++20) |
| Hilfstyp |
||
| template< class I > using prev_permutation_result = ranges::in_found_result<I>; |
(3) | (seit C++20) |
[first, last) in die vorherige Permutation, wobei die Menge aller Permutationen lexikographisch bezüglich des binären Vergleichsobjekts comp und des Projektionsobjekts proj geordnet ist.- {last, true}, wenn eine "vorherige" Permutation existiert. Andernfalls,
- {last, false}, und transformiert den Bereich in die (lexikographisch) letzte Permutation, als ob durch
ranges::sort(first, last, comp, proj); ranges::reverse(first, last);
Die auf dieser Seite beschriebenen funktionsähnlichen Entitäten sind Algorithmus-Funktionsobjekte (informell als niebloids bekannt), d.h.
- Können explizite Template-Argumentlisten bei keinem von ihnen angegeben werden.
- Keiner von ihnen ist für Argument-abhängige Suche sichtbar.
- Wenn einer von ihnen durch normale unqualifizierte Suche als Name links vom Funktionsaufrufoperator gefunden wird, wird die Argument-abhängige Suche unterdrückt.
Inhalt |
[edit] Parameter
| first, last | - | das Iterator-Sentinel-Paar, das den Bereich der zu "permutierenden" Elemente definiert |
| r | - | der Bereich der zu "permutierenden" Elemente |
| comp | - | Vergleichs-Funktionsobjekt, das true zurückgibt, wenn das erste Argument kleiner als das zweite ist |
| proj | - | Projektion, die auf die Elemente angewendet wird |
[edit] Rückgabewert
[edit] Ausnahmen
Alle Ausnahmen, die von Iterator-Operationen oder dem Vertauschen von Elementen geworfen werden.
[edit] Komplexität
Höchstens N / 2 Vertauschungen, wobei N ranges::distance(first, last) für Fall (1) oder ranges::distance(r) für Fall (2) ist. Im Durchschnitt über die gesamte Sequenz von Permutationen verwenden typische Implementierungen etwa 3 Vergleiche und 1,5 Vertauschungen pro Aufruf.
[edit] Hinweise
Implementierungen (z. B. MSVC STL) können Vektorisierung aktivieren, wenn der Iteratortyp contiguous_iterator modelliert und das Tauschen seines Werttyps weder eine nicht-triviale spezielle Memberfunktion noch eine ADL-gefundene swap aufruft.
[edit] Mögliche Implementierung
struct prev_permutation_fn { template<std::bidirectional_iterator I, std::sentinel_for<I> S, class Comp = ranges::less, class Proj = std::identity> requires std::sortable<I, Comp, Proj> constexpr ranges::prev_permutation_result<I> operator()(I first, S last, Comp comp = {}, Proj proj = {}) const { // check that the sequence has at least two elements if (first == last) return {std::move(first), false}; auto i{first}; ++i; if (i == last) return {std::move(i), false}; auto i_last{ranges::next(first, last)}; i = i_last; --i; // main "permutating" loop for (;;) { auto i1{i}; --i; if (std::invoke(comp, std::invoke(proj, *i1), std::invoke(proj, *i))) { auto j{i_last}; while (!std::invoke(comp, std::invoke(proj, *--j), std::invoke(proj, *i))) ; ranges::iter_swap(i, j); ranges::reverse(i1, last); return {std::move(i_last), true}; } // permutation "space" is exhausted if (i == first) { ranges::reverse(first, last); return {std::move(i_last), false}; } } } template<ranges::bidirectional_range R, class Comp = ranges::less, class Proj = std::identity> requires std::sortable<ranges::iterator_t<R>, Comp, Proj> constexpr ranges::prev_permutation_result<ranges::borrowed_iterator_t<R>> operator()(R&& r, Comp comp = {}, Proj proj = {}) const { return (*this)(ranges::begin(r), ranges::end(r), std::move(comp), std::move(proj)); } }; inline constexpr prev_permutation_fn prev_permutation {}; |
[edit] Beispiel
#include <algorithm> #include <array> #include <compare> #include <functional> #include <iostream> #include <string> struct S { char c{}; int i{}; auto operator<=>(const S&) const = default; friend std::ostream& operator<<(std::ostream& os, const S& s) { return os << "{'" << s.c << "', " << s.i << "}"; } }; auto print = [](auto const& v, char term = ' ') { std::cout << "{ "; for (const auto& e : v) std::cout << e << ' '; std::cout << '}' << term; }; int main() { std::cout << "Generate all permutations (iterators case):\n"; std::string s{"cba"}; do print(s); while (std::ranges::prev_permutation(s.begin(), s.end()).found); std::cout << "\nGenerate all permutations (range case):\n"; std::array a{'c', 'b', 'a'}; do print(a); while (std::ranges::prev_permutation(a).found); std::cout << "\nGenerate all permutations using comparator:\n"; using namespace std::literals; std::array z{"▁"s, "▄"s, "█"s}; do print(z); while (std::ranges::prev_permutation(z, std::greater()).found); std::cout << "\nGenerate all permutations using projection:\n"; std::array<S, 3> r{S{'C',1}, S{'B',2}, S{'A',3}}; do print(r, '\n'); while (std::ranges::prev_permutation(r, {}, &S::c).found); }
Ausgabe
Generate all permutations (iterators case):
{ c b a } { c a b } { b c a } { b a c } { a c b } { a b c }
Generate all permutations (range case):
{ c b a } { c a b } { b c a } { b a c } { a c b } { a b c }
Generate all permutations using comparator:
{ ▁ ▄ █ } { ▁ █ ▄ } { ▄ ▁ █ } { ▄ █ ▁ } { █ ▁ ▄ } { █ ▄ ▁ }
Generate all permutations using projection:
{ {'C', 1} {'B', 2} {'A', 3} }
{ {'C', 1} {'A', 3} {'B', 2} }
{ {'B', 2} {'C', 1} {'A', 3} }
{ {'B', 2} {'A', 3} {'C', 1} }
{ {'A', 3} {'C', 1} {'B', 2} }
{ {'A', 3} {'B', 2} {'C', 1} }[edit] Siehe auch
| (C++20) |
erzeugt die nächstgrößere lexikographische Permutation eines Bereichs von Elementen (Algorithmus-Funktionsobjekt) |
| (C++20) |
bestimmt, ob eine Sequenz eine Permutation einer anderen Sequenz ist (Algorithmus-Funktionsobjekt) |
| erzeugt die nächstgrößere lexikographische Permutation eines Bereichs von Elementen (Funktionsvorlage) | |
| erzeugt die nächstkleinere lexikographische Permutation eines Bereichs von Elementen (Funktionsvorlage) | |
| (C++11) |
bestimmt, ob eine Sequenz eine Permutation einer anderen Sequenz ist (Funktionsvorlage) |