std::includes
定义于头文件 <algorithm> |
||
template< class InputIt1, class InputIt2 > bool includes( InputIt1 first1, InputIt1 last1, |
(1) | (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > |
(2) | (since C++17) |
template< class InputIt1, class InputIt2, class Compare > bool includes( InputIt1 first1, InputIt1 last1, |
(3) | (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class Compare > |
(4) | (since 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&,但该函数不得修改传递给它的对象,并且必须能够接受类型(可能是 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++ 标准。
DR | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 1205 | C++98 | 如果 [ first2, last2) 为空,则返回值不明确 |
在这种情况下返回 true |
[编辑] 参见
计算两个集合之间的差集 (函数模板) | |
搜索某个元素范围的首次出现 (函数模板) | |
(C++20) |
如果一个序列是另一个序列的子序列,则返回 true (算法函数对象) |