命名空间
变体
操作

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

来自 cppreference.cn
< 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} }

[编辑] 参阅

生成元素范围的下一个更大的字典序排列
(算法函数对象)[编辑]
确定一个序列是否是另一个序列的排列
(算法函数对象)[编辑]
生成元素范围的下一个更大的字典序排列
(函数模板) [编辑]
生成元素范围的下一个更小的字典序排列
(函数模板) [编辑]
确定一个序列是否是另一个序列的排列
(函数模板) [编辑]