std::ranges:: next_permutation, std::ranges:: next_permutation_result
std::ranges
| Non-modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Partitioning operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sorting operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Binary search operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Set operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Heap operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Minimum/maximum operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Permutation operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Fold operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operations on uninitialized storage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Return types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<algorithm>
|
||
|
调用签名
|
||
|
template
<
std::
bidirectional_iterator
I,
std::
sentinel_for
<
I
>
S,
class
Comp
=
ranges::
less
,
class
Proj
=
std::
identity
>
|
(1) | (C++20 起) |
|
template
<
ranges::
bidirectional_range
R,
class
Comp
=
ranges::
less
,
class
Proj
=
std::
identity
>
|
(2) | (C++20 起) |
|
辅助类型
|
||
|
template
<
class
I
>
using next_permutation_result = ranges:: in_found_result < I > ; |
(3) | (C++20 起) |
[
first
,
last
)
转换为按字典序排列的下一个
排列
,其中所有排列的集合根据二元比较函数对象
comp
和投影函数对象
proj
进行排序。若存在这样的“下一个排列”,则返回
{
last,
true
}
;否则将范围转换为字典序首排列(操作等效于
ranges::
sort
(
first, last, comp, proj
)
),并返回
{
last,
false
}
。
本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:
目录 |
参数
| first, last | - | 定义待 排列 元素范围的迭代器-哨位对 |
| r | - |
待
排列
元素的
range
|
| comp | - | 比较 FunctionObject ,当第一个参数 小于 第二个参数时返回 true |
| proj | - | 应用于元素的投影 |
返回值
异常
任何从迭代器操作或元素交换抛出的异常。
复杂度
最多进行 \(\scriptsize N/2\) N / 2 次交换,其中 \(\scriptsize N\) N 在情况 (1) 中为 ranges:: distance ( first, last ) ,在情况 (2) 中为 ranges:: distance ( r ) 。在整个排列序列中平均计算,典型实现每次调用约使用 3 次比较和 1.5 次交换。
注释
实现(例如
MSVC STL
)可在迭代器类型满足
contiguous_iterator
概念且交换其值类型时既不调用非平凡特殊成员函数、也不通过
ADL
查找
swap
时启用向量化优化。
可能的实现
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 { // 检查序列是否至少包含两个元素 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}; // 主“排列”循环 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}; } // 排列“空间”已耗尽 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); }
输出:
生成所有排列(迭代器情况):
{ a b c } { a c b } { b a c } { b c a } { c a b } { c b a }
生成所有排列(范围情况):
{ a b c } { a c b } { b a c } { b c a } { c a b } { c b a }
使用比较器生成所有排列:
{ █ ▄ ▁ } { █ ▁ ▄ } { ▄ █ ▁ } { ▄ ▁ █ } { ▁ █ ▄ } { ▁ ▄ █ }
使用投影生成所有排列:
{ {'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} }
参见
|
(C++20)
|
生成元素范围的下一个较小字典序排列
(算法函数对象) |
|
(C++20)
|
判断序列是否为另一序列的排列
(算法函数对象) |
|
生成元素范围的下一个较大字典序排列
(函数模板) |
|
|
生成元素范围的下一个较小字典序排列
(函数模板) |
|
|
(C++11)
|
判断序列是否为另一序列的排列
(函数模板) |