Namespaces
Variants

std:: next_permutation

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
next_permutation

C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <algorithm>
template < class BidirIt >
bool next_permutation ( BidirIt first, BidirIt last ) ;
(1) (自 C++20 起为 constexpr)
template < class BidirIt, class Compare >
bool next_permutation ( BidirIt first, BidirIt last, Compare comp ) ;
(2) (自 C++20 起为 constexpr)

将区间 [ first , last ) 重新排列为下一个 排列 。若存在“下一个排列”则返回 true ;否则将区间变换为字典序首排列(如同通过 std::sort ),并返回 false

1) 所有排列的集合按字典序进行排序,排序依据为 operator < (C++20 前) std:: less { } (C++20 起)
2) 所有排列的集合按照 comp 进行字典序排序。

如果 * first 的类型不满足 Swappable 要求 (C++11 前) BidirIt 不满足 ValueSwappable 要求 (C++11 起) ,则行为未定义。

目录

参数

first, last - 定义要排列元素 范围 的迭代器对
comp - 比较函数对象(即满足 Compare 要求的对象),当第一个参数 小于 第二个时返回 true

比较函数的签名应等价于如下形式:

bool cmp ( const Type1 & a, const Type2 & b ) ;

虽然签名不需要包含 const & ,但函数不得修改传递给它的对象,且必须能够接受所有(可能为 const 的) Type1 Type2 类型的值,无论其 值类别 为何(因此不允许使用 Type1 & ,也不允许使用 Type1 ,除非对于 Type1 类型移动操作等价于拷贝操作 (C++11 起) )。
类型 Type1 Type2 必须使得 BidirIt 类型的对象在解引用后能隐式转换到这两个类型。

类型要求
-
BidirIt 必须满足 LegacyBidirectionalIterator 的要求。

返回值

若新排列按字典序大于原排列则为 true 。若已到达最终排列且范围已被重置为首个排列则为 false

复杂度

给定 N std:: distance ( first, last )

1,2) 最多进行
N
2
次交换。

异常

任何从迭代器操作或元素交换抛出的异常。

可能的实现

template<class BidirIt>
bool next_permutation(BidirIt first, BidirIt last)
{
    auto r_first = std::make_reverse_iterator(last);
    auto r_last = std::make_reverse_iterator(first);
    auto left = std::is_sorted_until(r_first, r_last);
    if (left != r_last)
    {
        auto right = std::upper_bound(r_first, left, *left);
        std::iter_swap(left, right);
    }
    std::reverse(left.base(), last);
    return left != r_last;
}

注释

在整个排列序列的平均情况下,典型实现每次调用大约使用3次比较和1.5次交换。

实现(例如 MSVC STL )可在迭代器类型满足 LegacyContiguousIterator 且交换其值类型时既不调用非平凡特殊成员函数,也不调用 ADL 查找到的 swap 时启用向量化。

示例

以下代码打印字符串 "aba" 的所有三种排列。

#include <algorithm>
#include <iostream>
#include <string>
int main()
{
    std::string s = "aba";
    do
    {
        std::cout << s << '\n';
    }
    while (std::next_permutation(s.begin(), s.end()));
    std::cout << s << '\n';
}

输出:

aba
baa
aab

参见

判断一个序列是否是另一个序列的排列
(函数模板)
生成元素范围的下一个较小字典序排列
(函数模板)
生成元素范围的下一个更大字典序排列
(算法函数对象)