名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | 演算法‎ | 範圍
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
受約束演算法
此選單中的所有名稱均屬於名稱空間 std::ranges
非修改序列操作
修改序列操作
劃分操作
排序操作
二分查詢操作(在已排序的範圍內)
       
       
集合操作(於已排序範圍上)
堆操作
最小/最大值操作
       
       
排列操作
next_permutation
  
摺疊操作
數值操作
(C++23)            
對未初始化儲存的操作
返回型別
 
定義於標頭檔案 <algorithm>
呼叫簽名 (Call signature)
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 = {} );
(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 next_permutation_result<ranges::borrowed_iterator_t<R>>

    next_permutation( R&& r, Comp comp = {}, Proj proj = {} );
(2) (C++20 起)
輔助型別
template< class I >
using next_permutation_result = ranges::in_found_result<I>;
(3) (C++20 起)
1) 將範圍 [firstlast) 轉換為下一個排列,其中所有排列集合根據二元比較函式物件 comp 和投影函式物件 proj字典序排序。如果存在這樣的“下一個排列”,則返回 {last, true};否則將範圍轉換為字典序的第一個排列,如同透過 ranges::sort(first, last, comp, proj),並返回 {last, false}
2)(1),但使用 r 作為源範圍,如同使用 ranges::begin(r) 作為 first,並使用 ranges::end(r) 作為 last

本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即

  • 呼叫它們中的任何一個時,不能指定顯式模板引數列表。
  • 它們中的任何一個都對 引數依賴查詢 不可見。
  • 當它們中的任何一個透過非限定查詢作為函式呼叫運算子左側的名稱被找到時,實參依賴查詢被抑制。

目錄

[編輯] 引數

first, last - 定義要排列元素的範圍的迭代器-哨兵對
r - 排列元素的範圍
comp - 比較函式物件,如果第一個引數小於第二個引數,則返回true
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 是情況 (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} }

[編輯] 參見

生成元素範圍的下一個更小的字典序排列
(演算法函式物件)[編輯]
確定一個序列是否是另一個序列的排列
(演算法函式物件)[編輯]
生成元素範圍的下一個更大的字典序排列
(函式模板) [編輯]
生成元素範圍的下一個更小的字典序排列
(函式模板) [編輯]
確定一個序列是否是另一個序列的排列
(函式模板) [編輯]