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
)
未按照
operator
<
(C++20 前)
std::
less
{
}
(C++20 起)
进行排序,则行为未定义。
[
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
&
,函数也不能修改传递给它的对象,而且必须能够接受(可能为 const 的)类型
|
| 类型要求 | ||
-
InputIt1, InputIt2
必须满足
LegacyInputIterator
的要求。
|
||
-
ForwardIt1, ForwardIt2
必须满足
LegacyForwardIterator
的要求。
|
||
-
Compare
必须满足
Compare
的要求。
|
||
返回值
当
[
first2
,
last2
)
是
[
first1
,
last1
)
的子序列时返回
true
;否则返回
false
。
空序列是任何序列的子序列,因此当
[
first2
,
last2
)
为空时将返回
true
。
复杂度
给定 N 1 为 std:: distance ( first1, last1 ) 且 N 2 为 std:: distance ( first2, last2 ) :
异常
带有名为
ExecutionPolicy
模板参数的重载按以下方式报告错误:
-
如果作为算法一部分调用的函数执行抛出异常,且
ExecutionPolicy是某个 标准策略 ,则调用 std::terminate 。对于任何其他ExecutionPolicy,其行为由实现定义。 - 如果算法无法分配内存,则抛出 std::bad_alloc 。
可能的实现
| 包含 (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; } |
| 包含 (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++ 标准。
| DR | 适用范围 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| LWG 1205 | C++98 |
当区间
[
first2
,
last2
)
为空时返回值不明确
|
此情况下返回 true |
参见
|
计算两个集合的差集
(函数模板) |
|
|
搜索元素范围的首次出现
(函数模板) |
|
|
(C++20)
|
若一个序列是另一个序列的子序列则返回
true
(算法函数对象) |