std::ranges::prev_permutation, std::ranges::prev_permutation_result
來自 cppreference.com
定義於標頭檔案 <algorithm> |
||
呼叫簽名 (Call signature) |
||
template< std::bidirectional_iterator I, std::sentinel_for<I> S, class Comp = ranges::less, class Proj = std::identity > |
(1) | (C++20 起) |
template< ranges::bidirectional_range R, class Comp = ranges::less, class Proj = std::identity > |
(2) | (C++20 起) |
輔助型別 |
||
template< class I > using prev_permutation_result = ranges::in_found_result<I>; |
(3) | (C++20 起) |
返回
- {last, true} 如果存在“上一個”排列。否則,
- {last, false},並將範圍轉換為(字典序)最後一個排列,如同透過
ranges::sort(first, last, comp, proj); ranges::reverse(first, last);
本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
目錄 |
[編輯] 引數
first, last | - | 定義要“排列”的元素範圍的迭代器-哨兵對 |
r | - | 要“排列”的元素range |
comp | - | 比較函式物件,如果第一個引數小於第二個引數,則返回true |
proj | - | 應用於元素的投影 |
[編輯] 返回值
1) 如果新排列在字典序上小於舊排列,則返回 ranges::prev_permutation_result<I>{last, true}。如果達到了第一個排列並且範圍被重置為最後一個排列,則返回 ranges::prev_permutation_result<I>{last, false}。
[編輯] 異常
迭代器操作或元素交換丟擲的任何異常。
[編輯] 複雜度
最多 N / 2 次交換,其中 N 是情況 (1) 中的 ranges::distance(first, last) 或情況 (2) 中的 ranges::distance(r)。在所有排列序列的平均情況下,典型實現每次呼叫大約需要 3 次比較和 1.5 次交換。
[編輯] 註解
當迭代器型別建模contiguous_iterator
且其值型別的交換不呼叫非平凡的特殊成員函式或ADL查詢的swap
時,實現(例如 MSVC STL)可能會啟用向量化。
[編輯] 可能實現
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 {}; |
[編輯] 示例
執行此程式碼
#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); }
輸出
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} }
[編輯] 參閱
(C++20) |
生成元素範圍的下一個更大的字典序排列 (演算法函式物件) |
(C++20) |
確定一個序列是否是另一個序列的排列 (演算法函式物件) |
生成元素範圍的下一個更大的字典序排列 (函式模板) | |
生成元素範圍的下一個更小的字典序排列 (函式模板) | |
(C++11) |
確定一個序列是否是另一個序列的排列 (函式模板) |