Namespaces
Variants

std::ranges::is_permutation

来自 zh.cppreference.net
 
 
算法库
受约束算法和范围上的算法 (C++20)
受约束算法,例如ranges::copy, ranges::sort, ...
排序和相关操作
划分操作
(C++11)    

排序操作
二分搜索操作
(在划分范围上)
集合操作(在有序范围上)
合并操作(在有序范围上)
堆操作
最小/最大操作
(C++11)
(C++17)
字典序比较操作
排列操作


 
受约束算法
此菜单中的所有名称都属于命名空间std::ranges
不修改序列的操作
修改序列的操作
划分操作
排序操作
二分搜索操作(在有序范围上)
       
       
集合操作(在有序范围上)
堆操作
最小/最大操作
       
       
排列操作
折叠操作
数值操作
(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) (自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) (自C++20起)
1)返回true如果存在一个排列中元素的排列[first1, last1)使得范围等于于[first2, last2) (在应用相应的投影Proj1、Proj2、并使用二元谓词Pred作为比较器后)。否则返回false。
2)与(1)相同,但使用r1作为第一个源范围,r2作为第二个源范围,如同使用ranges::begin(r1)作为first1、ranges::end(r1)作为last1、ranges::begin(r2)作为first2、ranges::end(r2)作为last2。

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

参数

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

返回值

当范围 [ first1 , last1 ) 是范围 [ first2 , last2 ) 的一个排列时返回 true 。

复杂度

最多进行 O(N 2 ) 次谓词和投影操作,若序列已相等则精确进行 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

另请参阅

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