std::ranges::next_permutation, std::ranges::next_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 next_permutation_result = ranges::in_found_result<I>; |
(3) | (C++20 起) |
1) 將範圍
[
first,
last)
轉換為下一個排列,其中所有排列集合根據二元比較函式物件 comp 和投影函式物件 proj 按字典序排序。如果存在這樣的“下一個排列”,則返回 {last, true};否則將範圍轉換為字典序的第一個排列,如同透過 ranges::sort(first, last, comp, proj),並返回 {last, false}。本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
目錄 |
[編輯] 引數
first, last | - | 定義要排列元素的範圍的迭代器-哨兵對 |
r | - | 要排列元素的範圍 |
comp | - | 比較函式物件,如果第一個引數小於第二個引數,則返回true |
proj | - | 應用於元素的投影 |
[編輯] 返回值
1) 如果新排列在字典序上大於舊排列,則返回 ranges::next_permutation_result<I>{last, true}。如果達到最後一個排列並將範圍重置為第一個排列,則返回 ranges::next_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 next_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::next_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}; I i_last{ranges::next(first, last)}; I i{i_last}; if (first == --i) return {std::move(i_last), false}; // main "permutating" loop for (;;) { I i1{i}; if (std::invoke(comp, std::invoke(proj, *--i), std::invoke(proj, *i1))) { I j{i_last}; while (!std::invoke(comp, std::invoke(proj, *i), std::invoke(proj, *--j))) {} std::iter_swap(i, j); std::reverse(i1, i_last); return {std::move(i_last), true}; } // permutation "space" is exhausted if (i == first) { std::reverse(first, i_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::next_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 next_permutation_fn next_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{"abc"}; do { print(s); } while (std::ranges::next_permutation(s.begin(), s.end()).found); std::cout << "\n" "Generate all permutations (range case):\n"; std::array a{'a', 'b', 'c'}; do { print(a); } while (std::ranges::next_permutation(a).found); std::cout << "\n" "Generate all permutations using comparator:\n"; using namespace std::literals; std::array z{"█"s, "▄"s, "▁"s}; do { print(z); } while (std::ranges::next_permutation(z, std::greater()).found); std::cout << "\n" "Generate all permutations using projection:\n"; std::array<S, 3> r{S{'A',3}, S{'B',2}, S{'C',1}}; do { print(r, '\n'); } while (std::ranges::next_permutation(r, {}, &S::c).found); }
輸出
Generate all permutations (iterators case): { a b c } { a c b } { b a c } { b c a } { c a b } { c b a } Generate all permutations (range case): { a b c } { a c b } { b a c } { b c a } { c a b } { c b a } Generate all permutations using comparator: { █ ▄ ▁ } { █ ▁ ▄ } { ▄ █ ▁ } { ▄ ▁ █ } { ▁ █ ▄ } { ▁ ▄ █ } Generate all permutations using projection: { {'A', 3} {'B', 2} {'C', 1} } { {'A', 3} {'C', 1} {'B', 2} } { {'B', 2} {'A', 3} {'C', 1} } { {'B', 2} {'C', 1} {'A', 3} } { {'C', 1} {'A', 3} {'B', 2} } { {'C', 1} {'B', 2} {'A', 3} }
[編輯] 參見
(C++20) |
生成元素範圍的下一個更小的字典序排列 (演算法函式物件) |
(C++20) |
確定一個序列是否是另一個序列的排列 (演算法函式物件) |
生成元素範圍的下一個更大的字典序排列 (函式模板) | |
生成元素範圍的下一個更小的字典序排列 (函式模板) | |
(C++11) |
確定一個序列是否是另一個序列的排列 (函式模板) |