Namespaces
Variants

std:: includes

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <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 起)

若已排序范围 [ first2 , last2 ) 是已排序范围 [ first1 , last1 ) 子序列 (子序列不必连续),则返回 true

1) 如果 [ first1 , last1 ) [ first2 , last2 ) 未按照 operator < (C++20 前) std:: less { } (C++20 起) 进行排序,则行为未定义。
3) 如果 [ first1 , last1 ) [ first2 , last2 ) 未按 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 & ,函数也不能修改传递给它的对象,而且必须能够接受(可能为 const 的)类型 Type1 Type2 的所有值,与 值类别 无关(从而不允许 Type1& ,也不允许 Type1 ,除非 Type1 的移动等价于复制 (C++11 起) )。
类型 Type1 Type2 必须使得 InputIt 类型的对象在解引用后能隐式转换到这两个类型。 ​

类型要求
-
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 )

1,2) 最多进行 2⋅(N 1 +N 2 )-1 次比较,使用 operator < (C++20 前) std:: less { } (C++20 起)
3,4) 最多需要 2⋅(N 1 +N 2 )-1 次比较函数 comp 的调用。

异常

带有名为 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

参见

计算两个集合的差集
(函数模板)
搜索元素范围的首次出现
(函数模板)
若一个序列是另一个序列的子序列则返回 true
(算法函数对象)