名稱空間
變體
操作

std::ranges::is_permutation

來自 cppreference.com
< cpp‎ | 演算法‎ | 範圍
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
受約束演算法
此選單中的所有名稱均屬於名稱空間 std::ranges
非修改序列操作
修改序列操作
劃分操作
排序操作
二分查詢操作(在已排序的範圍內)
       
       
集合操作(於已排序範圍上)
堆操作
最小/最大值操作
       
       
排列操作
is_permutation
    
摺疊操作
數值操作
(C++23)            
對未初始化儲存的操作
返回型別
 
定義於標頭檔案 <algorithm>
呼叫簽名 (Call signature)
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) 如果在範圍 [first1last1) 的元素中存在一個 排列,使得該範圍在應用相應的投影 Proj1Proj2 並使用二元謂詞 Pred 作為比較器後,與 [first2last2) 相等,則返回 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 - 第一個元素 範圍
r2 - 第二個元素 範圍
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 施加等價關係
(概念) [編輯]