Namespaces
Variants

std:: prev_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
prev_permutation

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

将范围 [ first , last ) 转换为前一个 排列 。若存在这样的排列则返回 true ,否则将范围转换为最后一个排列(如同通过 std::sort 后接 std::reverse 实现)并返回 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 必须满足 ValueSwappable LegacyBidirectionalIterator 的要求。

返回值

若新排列在字典序中位于旧排列之前,则为 true 。若已到达首个排列且范围被重置为末位排列,则为 false

异常

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

复杂度

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

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

可能的实现

template<class BidirIt>
bool prev_permutation(BidirIt first, BidirIt last)
{
    if (first == last)
        return false;
    BidirIt i = last;
    if (first == --i)
        return false;
    while (1)
    {
        BidirIt i1, i2;
        i1 = i;
        if (*i1 < *--i)
        {
            i2 = last;
            while (!(*--i2 < *i))
                ;
            std::iter_swap(i, i2);
            std::reverse(i1, last);
            return true;
        }
        if (i == first)
        {
            std::reverse(first, last);
            return false;
        }
    }
}

注释

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

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

示例

以下代码按逆序输出字符串 "cab" 的全部六种排列。

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

输出:

cab bca bac acb abc cba

参见

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