|
|
|
调用签名
|
|
|
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>
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 ranges::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) 变换范围 [first, last)
到前一排列,其中所有排列的集合在词法上相对按照函数对象 comp
与投影函数对象 proj
定序。若存在“前一排列”则返回 {last, true} ;否则变换范围到词法上的末排列(如同用 ranges::sort(first, last, comp, proj); ranges::reverse(first, last); ),并返回 {last, false} 。
2) 同 (1) ,但以 r
为源范围,如同以 ranges::begin(r) 为 first
并以 ranges::end(r) 为 last
。
此页面上描述的仿函数实体是 niebloid,即:
实际上,它们能以函数对象,或者某些特殊编译器扩展实现。
参数
first, last
|
-
|
要重排的元素范围
|
r
|
-
|
要重排的元素范围
|
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>> 。
异常
任何从迭代器操作或元素交换抛出的异常。
复杂度
至多交换 \(\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 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 {
// 检查序列是否拥有至少二个元素
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;
// 主“重排”循环
for (;;) {
auto i1 {i};
--i;
if (std::invoke(comp, std::invoke(proj, *i), std::invoke(proj, *i1))) {
auto j {i_last};
while (!std::invoke(comp, std::invoke(proj, *i), std::invoke(proj, *--j)))
{ }
ranges::iter_swap(i, j);
ranges::reverse(i1, last);
return {std::move(i_last), true};
}
// 耗尽重排“空格”
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 << "\n" "Generate all permutations (range case):\n";
std::array a{'c', 'b', 'a'};
do { print(a); } while(std::ranges::prev_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::prev_permutation(z, std::greater()).found);
std::cout << "\n" "Generate 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} }
参阅
|
产生某个元素范围的按字典序下一个较大的排列 (niebloid) |
|
确定一个序列是否为另一序列的重排 (niebloid) |
|
产生某个元素范围的按字典顺序的下一个较大的排列 (函数模板) |
|
产生某个元素范围的按字典顺序的下一个较小的排列 (函数模板) |
|
判断一个序列是否为另一个序列的排列 (函数模板) |