Namespaces
Variants

std::ranges::next_permutation, std::ranges::next_permutation_result

ja.cppreference.net より
 
 
アルゴリズムライブラリ
制約付きアルゴリズムと範囲に関するアルゴリズム (C++20)
制約付きアルゴリズム、例えば ranges::copy、ranges::sort、...
ソートと関連操作
分割操作
(C++11)    

ソート操作
二分探索操作
(分割された範囲に対する)
集合操作 (ソート済み範囲に対する)
マージ操作 (ソート済み範囲に対する)
ヒープ操作
最小値/最大値操作
(C++11)
(C++17)
辞書順比較操作
順列操作


 
制約付きアルゴリズム
このメニューのすべての名前は名前空間 std::ranges
に属します
(C++23)
unique_copy
stable_partition
partial_sort_copy
equal_range
set_symmetric_difference
pop_heap
clamp
prev_permutation
fold_left_first  
fold_right
fold_right_last  
fold_left_with_iter
fold_left_first_with_iter
(C++23)
iota            
(C++23)
generate_random
(C++26)
uninitialized_value_construct_n
 
(C++23)<algorithm>
ヘッダ
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 next_permutation_result<I>
    next_permutation( I first, S last, Comp comp = {}, Proj proj = {} );
で定義 呼び出しシグネチャ
template< ranges::bidirectional_range R, class Comp = ranges::less,
          class Proj = std::identity >
requires std::sortable<ranges::iterator_t<R>, Comp, Proj>
constexpr next_permutation_result<ranges::borrowed_iterator_t<R>>
    next_permutation( R&& r, Comp comp = {}, Proj proj = {} );
(1) (C++20 以降)
(2)
template< class I >
using next_permutation_result = ranges::in_found_result<I>;
(C++20 以降) ヘルパー型
(3) (C++20 以降)[first, last)1) 範囲 を辞書式順序で次の 順列に変換します。順序は二項比較関数オブジェクト comp と射影関数オブジェクト proj に従います。そのような {last, true}"次の順列" が存在する場合は を返します。そうでない場合は、範囲を辞書式順序で最初の順列 (昇順) に変換し、{last, false} を返します。
2) (1) と同じですが、 をソース範囲として使用し、あたかも を r として、ranges::begin(r) を first として、ranges::end(r) を last として使用する点が異なります。

このページに記述されている関数のようなエンティティは アルゴリズム関数オブジェクト (非公式には niebloid として知られる) です。つまり:

パラメータ

first, last - 要素を 順列変更 する範囲を定義するイテレータ-番兵ペア
r - 順列変更 する要素の range
comp - 第1引数が第2引数より 小さい 場合に true を返す比較 FunctionObject
proj - 要素に適用する射影

戻り値

1) ranges :: next_permutation_result < I > { last, true } 新しい順列が前の順列より辞書順で 大きい 場合。 ranges :: next_permutation_result < I > { last, false } 最後の順列に到達し、範囲が最初の順列にリセットされた場合。
2) (1) と同様であるが、戻り値の型が ranges :: next_permutation_result < ranges:: borrowed_iterator_t < R >> である点が異なる。

例外

イテレータ操作または要素の交換からスローされる例外。

計算量

最大 N / 2 回のスワップ。ここで N は ranges:: distance ( first, last ) の場合 (1) または ranges:: distance ( r ) の場合 (2) である。順列全体のシーケンスを平均すると、典型的な実装では呼び出しごとに約3回の比較と1.5回のスワップを使用する。

注記

実装(例: MSVC STL )は、イテレータ型が contiguous_iterator をモデルし、その値型の交換が非自明な特殊メンバ関数も ADL で発見された swap も呼び出さない場合、ベクトル化を有効化することがある。

実装例

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
    {
        // シーケンスに少なくとも2つの要素があることを確認
        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};
        // メインの「順列生成」ループ
        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};
            }
            // 順列の「空間」が尽きた
            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 << "すべての順列を生成(イテレータの場合):\n";
    std::string s{"abc"};
    do
    {
        print(s);
    }
    while (std::ranges::next_permutation(s.begin(), s.end()).found);
    std::cout << "\n" "すべての順列を生成(レンジの場合):\n";
    std::array a{'a', 'b', 'c'};
    do
    {
        print(a);
    }
    while (std::ranges::next_permutation(a).found);
    std::cout << "\n" "比較関数を使用してすべての順列を生成:\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" "射影を使用してすべての順列を生成:\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);
}

出力:

すべての順列を生成(イテレータの場合):
{ a b c } { a c b } { b a c } { b c a } { c a b } { c b a }
すべての順列を生成(レンジの場合):
{ a b c } { a c b } { b a c } { b c a } { c a b } { c b a }
比較関数を使用してすべての順列を生成:
{ █ ▄ ▁ } { █ ▁ ▄ } { ▄ █ ▁ } { ▄ ▁ █ } { ▁ █ ▄ } { ▁ ▄ █ }
射影を使用してすべての順列を生成:
{ {'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} }

関連項目

範囲の要素の、辞書順で次のより小さい順列を生成する
(アルゴリズム関数オブジェクト)
あるシーケンスが別のシーケンスの置換であるか判定する
(アルゴリズム関数オブジェクト)
範囲の要素の、辞書順で次のより大きい順列を生成する
(関数テンプレート)
範囲の要素の、辞書順で次のより小さい順列を生成する
(関数テンプレート & アルゴリズム関数オブジェクト)
あるシーケンスが別のシーケンスの置換であるか判定する
(関数テンプレート & アルゴリズム関数オブジェクト)