名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | 演算法‎ | 範圍
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
受約束演算法
此選單中的所有名稱均屬於名稱空間 std::ranges
非修改序列操作
修改序列操作
劃分操作
排序操作
二分查詢操作(在已排序的範圍內)
       
       
集合操作(於已排序範圍上)
堆操作
最小/最大值操作
       
       
排列操作
prev_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 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) 將範圍 [firstlast) 轉換為上一個排列,其中所有排列的集合是根據二元比較函式物件 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 - 比較函式物件,如果第一個引數小於第二個引數,則返回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 是情況 (1) 中的 ranges::distance(first, last) 或情況 (2) 中的 ranges::distance(r)。在所有排列序列的平均情況下,典型實現每次呼叫大約需要 3 次比較和 1.5 次交換。

[編輯] 註解

當迭代器型別建模contiguous_iterator且其值型別的交換不呼叫非平凡的特殊成員函式或ADL查詢的swap時,實現(例如 MSVC STL)可能會啟用向量化。

[編輯] 可能實現

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
    {
        // check that the sequence has at least two elements
        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;
        // main "permutating" loop
        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};
            }
            // permutation "space" is exhausted
            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 << "Generate all permutations (iterators case):\n";
    std::string s{"cba"};
    do print(s);
    while (std::ranges::prev_permutation(s.begin(), s.end()).found);
 
    std::cout << "\nGenerate all permutations (range case):\n";
    std::array a{'c', 'b', 'a'};
    do print(a);
    while (std::ranges::prev_permutation(a).found);
 
    std::cout << "\nGenerate all permutations using comparator:\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 << "\nGenerate all permutations using projection:\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);
}

輸出

Generate all permutations (iterators case):
{ c b a } { c a b } { b c a } { b a c } { a c b } { a b c }
Generate all permutations (range case):
{ c b a } { c a b } { b c a } { b a c } { a c b } { a b c }
Generate all permutations using comparator:
{ ▁ ▄ █ } { ▁ █ ▄ } { ▄ ▁ █ } { ▄ █ ▁ } { █ ▁ ▄ } { █ ▄ ▁ }
Generate all permutations using projection:
{ {'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} }

[編輯] 參閱

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