名稱空間
變體
操作

std::is_permutation

來自 cppreference.com
< cpp‎ | 演算法
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
is_permutation
(C++11)


C 庫
數值操作
未初始化記憶體上的操作
 
定義於標頭檔案 <algorithm>
template< class ForwardIt1, class ForwardIt2 >

bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,

                     ForwardIt2 first2 );
(1) (C++11 起)
(C++20 起為 constexpr)
template< class ForwardIt1, class ForwardIt2,

          class BinaryPredicate >
bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,

                     ForwardIt2 first2, BinaryPredicate p );
(2) (C++11 起)
(C++20 起為 constexpr)
template< class ForwardIt1, class ForwardIt2 >

bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,

                     ForwardIt2 first2, ForwardIt2 last2 );
(3) (C++14 起)
(C++20 起為 constexpr)
template< class ForwardIt1, class ForwardIt2,

          class BinaryPredicate >
bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,
                     ForwardIt2 first2, ForwardIt2 last2,

                     BinaryPredicate p );
(4) (C++14 起)
(C++20 起為 constexpr)

檢查範圍 [first1last1) 是否是始於 first2 的範圍的排列

  • 對於過載 (1,2),第二個範圍擁有 std::distance(first1, last1) 個元素。
  • 對於過載 (3,4),第二個範圍是 [first2last2)
1,3) 元素使用 operator== 比較。
2,4) 元素使用給定的二元謂詞 p 比較。

ForwardIt1ForwardIt2 有不同的值型別,則程式非良構。

若比較函式不是等價關係,則行為未定義。

目錄

[編輯] 引數

first1, last1 - 定義要比較的第一個元素範圍的迭代器對
first2, last2 - 定義要比較的第二個元素範圍的迭代器對
p - 二元謂詞,如果元素應被視為相等,則返回 true。

謂詞函式的簽名應等效於以下內容:

 bool pred(const Type1 &a, const Type2 &b);

雖然簽名不需要有 const &,但該函式絕不能修改傳遞給它的物件,且必須能接受 Type1Type2 型別(可能為 const)的所有值,而無論其值類別如何(因此,不允許 Type1 &,除非對於 Type1,移動等價於複製,否則也不允許 Type1(C++11 起))。
型別 Type1Type2 必須使型別 InputIt1InputIt2 的物件可以被解引用,然後隱式轉換為 Type1Type2。​

型別要求
-
ForwardIt1, ForwardIt2 必須滿足 LegacyForwardIterator 的要求。

[編輯] 返回值

若範圍 [first1last1) 是範圍 [first2last2) 的排列,則為 true,否則為 false

[編輯] 複雜度

給定 Nstd::distance(first1, last1)

1) 若兩個範圍相等,則使用 operator== 進行恰好 N 次比較,否則在最壞情況下進行 O(N2
)
次比較。
2) 若兩個範圍相等,則恰好應用謂詞 p N 次,否則在最壞情況下應用 O(N2
)
次。
3,4)ForwardIt1ForwardIt2 都是 LegacyRandomAccessIterator,且 last1 - first1 != last2 - first2true,則不會進行比較。
否則
3) 若兩個範圍相等,則使用 operator== 進行恰好 N 次比較,否則在最壞情況下進行 O(N2
)
次比較。
4) 若兩個範圍相等,則恰好應用謂詞 p N 次,否則在最壞情況下應用 O(N2
)
次。

[編輯] 可能的實現

template<class ForwardIt1, class ForwardIt2>
bool is_permutation(ForwardIt1 first, ForwardIt1 last,
                    ForwardIt2 d_first)
{
    // skip common prefix
    std::tie(first, d_first) = std::mismatch(first, last, d_first);
 
    // iterate over the rest, counting how many times each element
    // from [first, last) appears in [d_first, d_last)
    if (first != last)
    {
        ForwardIt2 d_last = std::next(d_first, std::distance(first, last));
        for (ForwardIt1 i = first; i != last; ++i)
        {
            if (i != std::find(first, i, *i))
                continue; // this *i has been checked
 
            auto m = std::count(d_first, d_last, *i);
            if (m == 0 || std::count(i, last, *i) != m)
                return false;
        }
    }
    return true;
}

[編輯] 注意

std::is_permutation 可用於測試,即檢查重排演算法(例如排序、洗牌、分割槽)的正確性。如果 x 是原始範圍而 y置換過的範圍,則 std::is_permutation(x, y) == true 意味著 y“相同”的元素組成,可能位於不同的位置。

[編輯] 示例

#include <algorithm>
#include <iostream>
 
template<typename Os, typename V>
Os& operator<<(Os& os, const V& v)
{
    os << "{ ";
    for (const auto& e : v)
        os << e << ' ';
    return os << '}';
}
 
int main()
{
    static constexpr auto v1 = {1, 2, 3, 4, 5};
    static constexpr auto v2 = {3, 5, 4, 1, 2};
    static constexpr auto v3 = {3, 5, 4, 1, 1};
 
    std::cout << v2 << " is a permutation of " << v1 << ": " << std::boolalpha
              << std::is_permutation(v1.begin(), v1.end(), v2.begin()) << '\n'
              << v3 << " is a permutation of " << v1 << ": "
              << std::is_permutation(v1.begin(), v1.end(), v3.begin()) << '\n';
}

輸出

{ 3 5 4 1 2 } is a permutation of { 1 2 3 4 5 }: true
{ 3 5 4 1 1 } is a permutation of { 1 2 3 4 5 }: false

[編輯] 參閱

生成元素範圍的下一個更大的字典序排列
(函式模板) [編輯]
生成元素範圍的下一個更小的字典序排列
(函式模板) [編輯]
指定 relation 施加等價關係
(概念) [編輯]
確定一個序列是否是另一個序列的排列
(演算法函式物件)[編輯]