std::ranges:: is_permutation
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::
forward_iterator
I1,
std::
sentinel_for
<
I1
>
S1,
std::
forward_iterator
I2,
std::
sentinel_for
<
I2
>
S2,
|
(1) | (C++20 起) |
|
template
<
ranges::
forward_range
R1,
ranges::
forward_range
R2,
class
Proj1
=
std::
identity
,
class
Proj2
=
std::
identity
,
|
(2) | (C++20 起) |
[
first1
,
last1
)
中元素的某种
排列
,使得该范围与范围
[
first2
,
last2
)
在应用对应投影
Proj1
、
Proj2
并使用二元谓词
Pred
作为比较器后
相等
,则返回
true
。否则返回
false
。
本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:
目录 |
参数
| first1, last1 | - | 定义第一个元素 范围 的迭代器-哨位对 |
| first2, last2 | - | 定义第二个元素 范围 的迭代器-哨位对 |
| r1 | - |
元素的第一个
range
|
| r2 | - |
元素的第二个
range
|
| pred | - | 应用于投影元素的谓词 |
| proj1 | - | 应用于第一个范围元素的投影 |
| proj2 | - | 应用于第二个范围元素的投影 |
返回值
当范围
[
first1
,
last1
)
是范围
[
first2
,
last2
)
的一个排列时返回
true
。
复杂度
最多进行 \(\scriptsize \mathcal{O}(N^2)\) O(N 2 ) 次谓词和投影操作,若序列已相等则精确进行 \(\scriptsize N\) N 次操作,其中 \(\scriptsize N\) N 为 ranges:: distance ( first1, last1 ) 。 但若 ranges:: distance ( first1, last1 ) ! = ranges:: distance ( first2, last2 ) ,则不执行任何谓词和投影操作。
注释
permutation 关系是一种 等价关系 。
ranges::is_permutation
可用于测试场景,例如检验重排算法(如排序、洗牌、分区)的正确性。若
p
是原始序列而
q
是“变异”序列,则
ranges
::
is_permutation
(
p, q
)
==
true
表示
q
由与
p
“相同”的元素(可能经过排列)构成。
可能的实现
struct is_permutation_fn { template<std::forward_iterator I1, std::sentinel_for<I1> S1, std::forward_iterator I2, std::sentinel_for<I2> S2, class Proj1 = std::identity, class Proj2 = std::identity, std::indirect_equivalence_relation<std::projected<I1, Proj1>, std::projected<I2, Proj2>> Pred = ranges::equal_to> constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const { // 跳过公共前缀 auto ret = std::ranges::mismatch(first1, last1, first2, last2, std::ref(pred), std::ref(proj1), std::ref(proj2)); first1 = ret.in1, first2 = ret.in2; // 遍历剩余部分,统计每个元素 // 在 [first1, last1) 中出现的次数与在 [first2, last2) 中出现的次数 for (auto i {first1}; i != last1; ++i) { const auto i_proj {std::invoke(proj1, *i)}; auto i_cmp = [&]<typename T>(T&& t) { return std::invoke(pred, i_proj, std::forward<T>(t)); }; if (i != ranges::find_if(first1, i, i_cmp, proj1)) continue; // 此 *i 已被检查 if (const auto m {ranges::count_if(first2, last2, i_cmp, proj2)}; m == 0 or m != ranges::count_if(i, last1, i_cmp, proj1)) return false; } return true; } template<ranges::forward_range R1, ranges::forward_range R2, class Proj1 = std::identity, class Proj2 = std::identity, std::indirect_equivalence_relation< std::projected<ranges::iterator_t<R1>, Proj1>, std::projected<ranges::iterator_t<R2>, Proj2>> Pred = ranges::equal_to> constexpr bool operator()(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const { return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), std::move(pred), std::move(proj1), std::move(proj2)); } }; inline constexpr is_permutation_fn is_permutation {}; |
示例
#include <algorithm> #include <array> #include <cmath> #include <iostream> #include <ranges> auto& operator<<(auto& os, std::ranges::forward_range auto const& v) { os << "{ "; for (const auto& e : v) os << e << ' '; return os << "}"; } int main() { static constexpr auto r1 = {1, 2, 3, 4, 5}; static constexpr auto r2 = {3, 5, 4, 1, 2}; static constexpr auto r3 = {3, 5, 4, 1, 1}; static_assert( std::ranges::is_permutation(r1, r1) && std::ranges::is_permutation(r1, r2) && std::ranges::is_permutation(r2, r1) && std::ranges::is_permutation(r1.begin(), r1.end(), r2.begin(), r2.end())); std::cout << std::boolalpha << "is_permutation(" << r1 << ", " << r2 << "): " << std::ranges::is_permutation(r1, r2) << '\n' << "is_permutation(" << r1 << ", " << r3 << "): " << std::ranges::is_permutation(r1, r3) << '\n' << "is_permutation with custom predicate and projections: " << std::ranges::is_permutation( std::array {-14, -11, -13, -15, -12}, // 第一范围 std::array {'F', 'E', 'C', 'B', 'D'}, // 第二范围 [](int x, int y) { return abs(x) == abs(y); }, // 谓词 [](int x) { return x + 10; }, // 第一范围的投影 [](char y) { return int(y - 'A'); }) // 第二范围的投影 << '\n'; }
输出:
is_permutation({ 1 2 3 4 5 }, { 3 5 4 1 2 }): true
is_permutation({ 1 2 3 4 5 }, { 3 5 4 1 1 }): false
is_permutation with custom predicate and projections: true
参见
|
(C++20)
|
生成元素范围的下一个更大字典序排列
(算法函数对象) |
|
(C++20)
|
生成元素范围的下一个更小字典序排列
(算法函数对象) |
|
(C++11)
|
判断一个序列是否是另一个序列的排列
(函数模板) |
|
生成元素范围的下一个更大字典序排列
(函数模板) |
|
|
生成元素范围的下一个更小字典序排列
(函数模板) |
|
|
(C++20)
|
指定
relation
施加等价关系
(概念) |