std::ranges::next_permutation, std::ranges::next_permutation_result
From cppreference.com
| ヘッダー <algorithm> で定義 |
||
| 呼び出しシグネチャ |
||
| 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} を返します。2) (1) と同じですが、ソース範囲として `r` を使用し、`ranges::begin(r)` を `first`、`ranges::end(r)` を `last` として扱います。
このページで説明されている関数のようなエンティティは、アルゴリズム関数オブジェクト(非公式にはニーブロイドとして知られている)です。つまり、
- これらのいずれかを呼び出す際に、明示的なテンプレート引数リストを指定することはできません。
- これらのいずれも実引数依存の名前探索には見えません。
- これらのいずれかが関数呼び出し演算子の左側の名前として通常の非修飾名探索によって見つかった場合、実引数依存の名前探索は抑制されます。
目次 |
[edit] Parameters
| first, last | - | 順列を生成する要素の範囲を定義するイテレータ-センチネルペア |
| r | - | 順列を生成するrange |
| comp | - | 最初の引数が2番目の引数より*小さい*場合に true を返す比較FunctionObject |
| proj | - | 要素に適用する射影 |
[edit] Return value
1) 新しい順列が古い順列より辞書順で*大きい*場合、 ranges::next_permutation_result<I>{last, true}。最後の順列に到達し、範囲が最初の順列にリセットされた場合、 ranges::next_permutation_result<I>{last, false}。
[edit] Exceptions
イテレータ操作または要素のスワップからスローされるすべての例外。
[edit] Complexity
N / 2 回以下のスワップ。ここで N は ranges::distance(first, last) ((1) の場合)または ranges::distance(r) ((2) の場合)です。順列の全シーケンスにわたる平均では、典型的な実装では呼び出しあたり約 3 回の比較と 1.5 回のスワップが使用されます。
[edit] Notes
イテレータ型が contiguous_iterator をモデル化し、その値型のスワップが自明でない特殊メンバ関数も ADL で見つかった swap も呼び出さない場合、実装 (例: MSVC STL) はベクトル化を有効にできる。
[edit] Possible implementation
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 {}; |
[edit] Example
このコードを実行
#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} }[edit] See also
| (C++20) |
要素の範囲の次に小さい辞書順の順列を生成する (アルゴリズム関数オブジェクト) |
| (C++20) |
あるシーケンスが別のシーケンスの順列であるかを判断する (アルゴリズム関数オブジェクト) |
| 要素の範囲の次に大きい辞書順の順列を生成する (関数テンプレート) | |
| 要素の範囲の次に小さい辞書順の順列を生成する (関数テンプレート) | |
| (C++11) |
あるシーケンスが別のシーケンスの順列であるかを判断する (関数テンプレート) |