命名空间
变体
操作

std::ranges::is_permutation

来自 cppreference.cn
< cpp‎ | 算法‎ | ranges
 
 
算法库
受约束算法和范围上的算法 (C++20)
受约束算法,例如 ranges::copy, ranges::sort, ...
执行策略 (C++17)
排序和相关操作
划分操作
排序操作
二分搜索操作
(在已划分的范围上)
集合操作 (在已排序的范围上)
合并操作 (在已排序的范围上)
堆操作
最小值/最大值操作
(C++11)
(C++17)
字典序比较操作
排列操作
C 库
数值操作
未初始化内存上的操作
 
受约束算法
此菜单中的所有名称都属于命名空间 std::ranges
非修改序列操作
修改序列操作
划分操作
排序操作
二分搜索操作 (在已排序的范围上)
       
       
集合操作 (在已排序的范围上)
堆操作
最小值/最大值操作
       
       
排列操作
is_permutation
    
折叠操作
数值操作
(C++23)            
未初始化存储上的操作
返回类型
 
定义于头文件 <algorithm>
调用签名
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
    is_permutation( I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},

                    Proj1 proj1 = {}, Proj2 proj2 = {} );
(1) (since C++20)
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
    is_permutation( R1&& r1, R2&& r2, Pred pred = {},

                    Proj1 proj1 = {}, Proj2 proj2 = {} );
(2) (since C++20)
1) 如果存在范围 [first1last1) 中元素的排列,使得该范围等于范围 [first2last2) (在应用相应的投影 Proj1, Proj2,并使用二元谓词 Pred 作为比较器之后),则返回 true。否则返回 false
2)(1) 相同,但使用 r1 作为第一个源范围,r2 作为第二个源范围,如同使用 ranges::begin(r1) 作为 first1ranges::end(r1) 作为 last1ranges::begin(r2) 作为 first2,以及 ranges::end(r2) 作为 last2

此页面上描述的类似函数的实体是算法函数对象 (非正式地称为 niebloids),即

目录

[编辑] 参数

first1, last1 - 定义第一个范围的元素的迭代器-哨位对
first2, last2 - 定义第二个范围的元素的迭代器-哨位对
r1 - 第一个range的元素
r2 - 第二个range的元素
pred - 应用于投影元素的谓词
proj1 - 应用于第一个范围中元素的投影
proj2 - 应用于第二个范围中元素的投影

[编辑] 返回值

如果范围 [first1last1) 是范围 [first2last2) 的排列,则返回 true

[编辑] 复杂度

最多 O(N2) 次谓词和每个投影的应用,或者如果序列已经相等,则正好 N 次,其中 Nranges::distance(first1, last1)。但是,如果 ranges::distance(first1, last1) != ranges::distance(first2, last2),则不应用谓词和投影。

[编辑] 注解

排列关系是等价关系

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
    {
        // skip common prefix
        auto ret = std::ranges::mismatch(first1, last1, first2, last2,
                                         std::ref(pred), std::ref(proj1), std::ref(proj2));
        first1 = ret.in1, first2 = ret.in2;
 
        // iterate over the rest, counting how many times each element
        // from [first1, last1) appears in [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; // this *i has been checked
 
            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},  // 1st range
            std::array {'F', 'E', 'C', 'B', 'D'},  // 2nd range
            [](int x, int y) { return abs(x) == abs(y); }, // predicate
            [](int x) { return x + 10; },          // projection for 1st range
            [](char y) { return int(y - 'A'); })   // projection for 2nd range
        << '\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

[编辑] 参见

生成元素范围的下一个更大的字典序排列
(算法函数对象)[编辑]
生成元素范围的下一个更小的字典序排列
(算法函数对象)[编辑]
确定一个序列是否是另一个序列的排列
(函数模板) [编辑]
生成元素范围的下一个更大的字典序排列
(函数模板) [编辑]
生成元素范围的下一个更小的字典序排列
(函数模板) [编辑]
指定 relation 施加等价关系
(概念) [编辑]