命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | algorithm‎ | ranges
 
 
算法库
约束算法和范围上的算法 (C++20)
约束算法,例如 ranges::copy, ranges::sort, ...
执行策略 (C++17)
排序及相关操作
划分操作
排序操作
二分搜索操作
(在已划分范围上)
集合操作(在已排序范围上)
合并操作(在已排序范围上)
堆操作
最小值/最大值操作
(C++11)
(C++17)
字典序比较操作
排列操作
C 库
数值操作
未初始化内存上的操作
 
约束算法
此菜单中的所有名称都属于命名空间 std::ranges
非修改序列操作
修改序列操作
划分操作
排序操作
二分搜索操作(在已排序范围上)
       
       
集合操作(在已排序范围上)
堆操作
最小值/最大值操作
       
       
排列操作
next_permutation
  
折叠操作
数值操作
(C++23)            
未初始化存储上的操作
返回类型
 
定义于头文件 <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 next_permutation_result<I>

    next_permutation( I first, S last, Comp comp = {}, Proj proj = {} );
(1) (since 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) (since C++20)
辅助类型
template< class I >
using next_permutation_result = ranges::in_found_result<I>;
(3) (since 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 - 排列的元素range
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 次交换,其中 Nranges::distance(first, last) (在情况 (1) 中)或 ranges::distance(r) (在情况 (2) 中)。在整个排列序列上平均,典型的实现每次调用使用大约 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} }

[编辑] 参见

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