Namespaces
Variants

std::includes

来自 zh.cppreference.net
 
 
算法库
受约束的算法和范围上的算法 (C++20)
受约束的算法,例如 ranges::copy, ranges::sort,...
排序和相关操作
划分操作
(C++11)    

排序操作
二分搜索操作
(在划分好的范围上)
集合操作(在排序范围上)
合并操作(在排序范围上)
堆操作
最小/最大操作
(C++11)
(C++17)
字典序比较操作
排列操作


 
定义于头文件 <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 起)

如果排序范围 true 是排序范围 [first2, last2) 的一个子序列(子序列不必连续),则返回 [first1, last1)。

1) 如果 [first1, last1) 或 [first2, last2) 未排序 相对于 operator<(C++20 前)std::less{}(C++20 起),则行为未定义。
3) 如果 [first1, last1) 或 [first2, last2) 未按照 comp 排序,则行为未定义。
2,4) 与 (1,3) 相同,但按照 policy 执行。
这些重载只有在以下表达式的值为 true 时才会参与重载决议:

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

(C++20 前)

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

(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

参见

计算两个集合的差
(函数模板 & 算法函数对象)
搜索元素范围的首次出现
(函数模板 & 算法函数对象)
判断一个序列是否是另一个的子序列
(算法函数对象)