Namespaces
Variants

std:: next_permutation

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
next_permutation

C library
Numeric operations
Operations on uninitialized memory
定義先ヘッダ <algorithm>
template < class BidirIt >
bool next_permutation ( BidirIt first, BidirIt last ) ;
(1) (constexpr since C++20)
template < class BidirIt, class Compare >
bool next_permutation ( BidirIt first, BidirIt last, Compare comp ) ;
(2) (constexpr since C++20)

範囲 [ first , last ) を次の 順列 に並べ替えます。そのような「次の順列」が存在する場合は true を返します。それ以外の場合は、範囲を辞書順で最初の順列に変換し( std::sort によって行われるように)、 false を返します。

1) すべての順列の集合は、 operator < (C++20以前) std:: less { } (C++20以降) に関して辞書順に並べられている。
2) すべての順列の集合は、 comp に関して辞書式順序で並べられます。

* first の型が Swappable でない場合 (C++11以前) BidirIt ValueSwappable でない場合 (C++11以降) 、動作は未定義です。

目次

パラメータ

first, last - 要素を並べ替える範囲を定義するイテレータのペア
comp - 比較関数オブジェクト(つまり Compare の要件を満たすオブジェクト)。最初の引数が2番目の引数より 小さい 場合に true を返す。

比較関数のシグネチャは以下と同等であるべき:

bool cmp ( const Type1 & a, const Type2 & b ) ;

シグネチャが const & を持つ必要はないが、関数は渡されたオブジェクトを変更してはならず、 値カテゴリ に関係なく型(おそらくconst) Type1 Type2 のすべての値を受け入れられなければならない(したがって、 Type1 & は許可されない 、また Type1 Type1 に対してムーブがコピーと等価である場合を除く)も許可されない (C++11以降) )。
Type1 Type2 は、 BidirIt 型のオブジェクトが間接参照可能で、それら両方に暗黙的に変換可能でなければならない。

型要件
-
BidirIt LegacyBidirectionalIterator の要件を満たさなければならない。

戻り値

true 新しい順列が辞書順で前の順列より大きい場合。 false 最後の順列に到達し、範囲が最初の順列にリセットされた場合。

計算量

与えられた N std:: distance ( first, last ) として:

1,2) 最大
N
2
回のスワップ。

例外

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

実装例

template<class BidirIt>
bool next_permutation(BidirIt first, BidirIt last)
{
    auto r_first = std::make_reverse_iterator(last);
    auto r_last = std::make_reverse_iterator(first);
    auto left = std::is_sorted_until(r_first, r_last);
    if (left != r_last)
    {
        auto right = std::upper_bound(r_first, left, *left);
        std::iter_swap(left, right);
    }
    std::reverse(left.base(), last);
    return left != r_last;
}
**注記**: 提供されたHTMLコードはC++のコードスニペットを含んでおり、指示に従って以下の通り対応しました: - HTMLタグと属性は一切翻訳せず、元のフォーマットを保持 - ` `, `
`, `` タグ内のテキストは翻訳対象外
- C++固有の用語(関数名、キーワード、型名など)は翻訳せず保持
上記のコードブロックは翻訳対象外の要素のみを含んでいるため、日本語への翻訳は行われていません。

注記

順列の全シーケンスにわたって平均すると、典型的な実装では呼び出しごとに約3回の比較と1.5回のスワップが使用されます。

実装(例: MSVC STL )は、イテレータ型が LegacyContiguousIterator を満たし、その値型の交換が非自明な特殊メンバ関数も ADL で見つかった swap も呼び出さない場合に、ベクトル化を有効化する可能性があります。

以下のコードは、文字列 "aba" の3つの順列をすべて出力します。

#include <algorithm>
#include <iostream>
#include <string>
int main()
{
    std::string s = "aba";
    do
    {
        std::cout << s << '\n';
    }
    while (std::next_permutation(s.begin(), s.end()));
    std::cout << s << '\n';
}

出力:

aba
baa
aab

関連項目

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