Namespaces
Variants

std::ranges:: prev_permutation, std::ranges:: prev_permutation_result

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
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
ヘッダーで定義 <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 prev_permutation_result < I >

prev_permutation ( I first, S last, Comp comp = { } , Proj proj = { } ) ;
(1) (C++20以降)
template < ranges:: bidirectional_range R, class Comp = ranges:: less ,

class Proj = std:: identity >
requires std:: sortable < ranges:: iterator_t < R > , Comp, Proj >
constexpr prev_permutation_result < ranges:: borrowed_iterator_t < R >>

prev_permutation ( R && r, Comp comp = { } , Proj proj = { } ) ;
(2) (C++20以降)
ヘルパー型
template < class I >
using prev_permutation_result = ranges:: in_found_result < I > ;
(3) (C++20以降)
1) 範囲 [ first , last ) を前の 順列 に変換する。ここで全ての順列の集合は、二項比較関数オブジェクト comp と射影関数オブジェクト proj に関して 辞書順 に並べられている。
戻り値:
  • { last, true } 「前の」順列が存在する場合。それ以外の場合、
  • { last, false } を返し、範囲を(辞書順的に)最後の順列に変換する。以下の操作と同等:
ranges::sort(first, last, comp, proj);
ranges::reverse(first, last);
2) (1) と同様だが、 r をソース範囲として使用する。 ranges:: begin ( r ) first として、 ranges:: end ( r ) last として使用するかのように動作する。

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

目次

パラメータ

first, last - 要素を「並べ替える」範囲を定義する イテレータ-番兵 のペア
r - 並べ替える要素の range
comp - 比較 FunctionObject で、最初の引数が2番目の引数より小さい場合に true を返す
proj - 要素に適用する射影

戻り値

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

例外

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

計算量

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

注記

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

実装例

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

出力:

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

関連項目

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