名稱空間
變體
操作

std::includes

來自 cppreference.com
< cpp‎ | 演算法
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
定義於標頭檔案 <algorithm>
template< class InputIt1, class InputIt2 >

bool includes( InputIt1 first1, InputIt1 last1,

               InputIt2 first2, InputIt2 last2 );
(1) (C++20 起為 constexpr)
template< class ExecutionPolicy,

          class ForwardIt1, class ForwardIt2 >
bool includes( ExecutionPolicy&& policy,
               ForwardIt1 first1, ForwardIt1 last1,

               ForwardIt2 first2, ForwardIt2 last2 );
(2) (C++17 起)
template< class InputIt1, class InputIt2, class Compare >

bool includes( InputIt1 first1, InputIt1 last1,

               InputIt2 first2, InputIt2 last2, Compare comp );
(3) (C++20 起為 constexpr)
template< class ExecutionPolicy,

          class ForwardIt1, class ForwardIt2, class Compare >
bool includes( ExecutionPolicy&& policy,
               ForwardIt1 first1, ForwardIt1 last1,

               ForwardIt2 first2, ForwardIt2 last2, Compare comp );
(4) (C++17 起)

如果已排序的範圍 [first2last2) 是已排序的範圍 [first1last1)子序列(子序列不必連續),則返回 true

1) 如果 [first1last1)[first2last2) 未根據 operator<(C++20 前)std::less{}(C++20 起) 進行排序,則行為未定義。
3) 如果 [first1last1)[first2last2) 未根據 comp 進行排序,則行為未定義。
2,4)(1,3),但按 policy 執行。
僅當滿足所有以下條件時,這些過載才參與過載決議

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>true

(C++20 前)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>true

(C++20 起)

目錄

[編輯] 引數

first1, last1 - 定義要檢查的元素已排序範圍的迭代器對
first2, last2 - 定義要搜尋的元素已排序範圍的迭代器對
policy - 要使用的 執行策略
comp - 比較函式物件(即滿足 Compare 要求的物件),如果第一個引數“小於”(即“排在”第二個引數之前),則返回 true

比較函式的簽名應等效於以下內容

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

雖然簽名不需要包含 const&,但函式不得修改傳遞給它的物件,並且必須能夠接受 Type1Type2 型別(可能是 const)的所有值,無論其值類別如何(因此,不允許使用 Type1&,除非對於 Type1,移動等同於複製(C++11 起))。
型別 Type1Type2 必須使得型別為 InputIt 的物件可以被解引用,然後隱式轉換為這兩種型別。​

型別要求
-
InputIt1, InputIt2 必須滿足 LegacyInputIterator 的要求。
-
ForwardIt1, ForwardIt2 必須滿足 LegacyForwardIterator 的要求。
-
Compare 必須滿足 Compare 的要求。

[編輯] 返回值

如果 [first2last2)[first1last1) 的子序列,則為 true;否則為 false

空序列是任何序列的子序列,因此如果 [first2last2) 為空,則返回 true

[編輯] 複雜度

給定 N1std::distance(first1, last1)N2std::distance(first2, last2)

1,2) 最多 2⋅(N1+N2)-1 次使用 operator<(C++20 前)std::less{}(C++20 起) 進行比較。
3,4) 最多 2⋅(N1+N2)-1 次呼叫比較函式 comp

[編輯] 異常

帶有模板引數 ExecutionPolicy 的過載按如下方式報告錯誤

  • 如果在演算法作為部分呼叫的函式執行時丟擲異常,並且 ExecutionPolicy標準策略之一,則呼叫 std::terminate。對於任何其他 ExecutionPolicy,行為是實現定義的。
  • 如果演算法未能分配記憶體,則丟擲 std::bad_alloc

[編輯] 可能的實現

include (1)
template<class InputIt1, class InputIt2>
bool includes(InputIt1 first1, InputIt1 last1,
              InputIt2 first2, InputIt2 last2)
{
    for (; first2 != last2; ++first1)
    {
        if (first1 == last1 || *first2 < *first1)
            return false;
        if (!(*first1 < *first2))
            ++first2;
    }
    return true;
}
include (3)
template<class InputIt1, class InputIt2, class Compare>
bool includes(InputIt1 first1, InputIt1 last1,
              InputIt2 first2, InputIt2 last2, Compare comp)
{
    for (; first2 != last2; ++first1)
    {
        if (first1 == last1 || comp(*first2, *first1))
            return false;
        if (!comp(*first1, *first2))
            ++first2;
    }
    return true;
}

[編輯] 示例

#include <algorithm>
#include <cctype>
#include <iostream>
 
template<class Os, class Co>
Os& operator<<(Os& os, const Co& v)
{
    for (const auto& i : v)
        os << i << ' ';
    return os << '\t';
}
 
int main()
{
    const auto
        v1 = {'a', 'b', 'c', 'f', 'h', 'x'},
        v2 = {'a', 'b', 'c'},
        v3 = {'a', 'c'},
        v4 = {'a', 'a', 'b'},
        v5 = {'g'},
        v6 = {'a', 'c', 'g'},
        v7 = {'A', 'B', 'C'};
 
    auto no_case = [](char a, char b) { return std::tolower(a) < std::tolower(b); };
 
    std::cout
    << v1 << "\nincludes:\n" << std::boolalpha
    << v2 << ": " << std::includes(v1.begin(), v1.end(), v2.begin(), v2.end()) << '\n'
    << v3 << ": " << std::includes(v1.begin(), v1.end(), v3.begin(), v3.end()) << '\n'
    << v4 << ": " << std::includes(v1.begin(), v1.end(), v4.begin(), v4.end()) << '\n'
    << v5 << ": " << std::includes(v1.begin(), v1.end(), v5.begin(), v5.end()) << '\n'
    << v6 << ": " << std::includes(v1.begin(), v1.end(), v6.begin(), v6.end()) << '\n'
    << v7 << ": " << std::includes(v1.begin(), v1.end(), v7.begin(), v7.end(), no_case)
          << " (case-insensitive)\n";
}

輸出

a b c f h x
includes:
a b c   : true
a c     : true
a a b   : false
g       : false
a c g   : false
A B C   : true (case-insensitive)

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 1205 C++98 如果 [first2last2) 為空,返回值不明確 在此情況下返回 true

[編輯] 另請參閱

計算兩個集合的差集
(函式模板) [編輯]
搜尋一個範圍的元素首次出現的位置
(函式模板) [編輯]
如果一個序列是另一個序列的子序列,則返回 true
(演算法函式物件)[編輯]