std::includes
定義於標頭檔案 <algorithm> |
||
template< class InputIt1, class InputIt2 > bool includes( InputIt1 first1, InputIt1 last1, |
(1) | (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > |
(2) | (C++17 起) |
template< class InputIt1, class InputIt2, class Compare > bool includes( InputIt1 first1, InputIt1 last1, |
(3) | (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class Compare > |
(4) | (C++17 起) |
如果已排序的範圍 [
first2,
last2)
是已排序的範圍 [
first1,
last1)
的子序列(子序列不必連續),則返回 true。
[
first1,
last1)
或 [
first2,
last2)
未根據 comp 進行排序,則行為未定義。
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&,但函式不得修改傳遞給它的物件,並且必須能夠接受 |
型別要求 | ||
-InputIt1, InputIt2 必須滿足 LegacyInputIterator 的要求。 | ||
-ForwardIt1, ForwardIt2 必須滿足 LegacyForwardIterator 的要求。 | ||
-Compare 必須滿足 Compare 的要求。 |
[編輯] 返回值
如果 [
first2,
last2)
是 [
first1,
last1)
的子序列,則為 true;否則為 false。
空序列是任何序列的子序列,因此如果 [
first2,
last2)
為空,則返回 true。
[編輯] 複雜度
給定 N1 為 std::distance(first1, last1),N2 為 std::distance(first2, last2)
[編輯] 異常
帶有模板引數 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 | 如果 [ first2, last2) 為空,返回值不明確 |
在此情況下返回 true |
[編輯] 另請參閱
計算兩個集合的差集 (函式模板) | |
搜尋一個範圍的元素首次出現的位置 (函式模板) | |
(C++20) |
如果一個序列是另一個序列的子序列,則返回 true (演算法函式物件) |