命名空间
变体
操作

std::find_first_of

来自 cppreference.cn
< cpp‎ | algorithm
 
 
算法库
受约束算法和范围上的算法 (C++20)
受约束算法,例如 ranges::copy, ranges::sort, ...
执行策略 (C++17)
排序和相关操作
划分操作
排序操作
二分搜索操作
(在已划分范围上)
集合操作 (在已排序范围上)
合并操作 (在已排序范围上)
堆操作
最小值/最大值操作
(C++11)
(C++17)
字典序比较操作
排列操作
C 库
数值操作
未初始化内存上的操作
 
定义于头文件 <algorithm>
template< class InputIt, class ForwardIt >

InputIt find_first_of( InputIt first, InputIt last,

                       ForwardIt s_first, ForwardIt s_last );
(1) (constexpr 从 C++20 起)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >

ForwardIt1 find_first_of( ExecutionPolicy&& policy,
                          ForwardIt1 first, ForwardIt1 last,

                          ForwardIt2 s_first, ForwardIt2 s_last );
(2) (自 C++17 起)
template< class InputIt, class ForwardIt, class BinaryPred >

InputIt find_first_of( InputIt first, InputIt last,
                       ForwardIt s_first, ForwardIt s_last,

                       BinaryPred p );
(3) (constexpr 从 C++20 起)
template< class ExecutionPolicy,

          class ForwardIt1, class ForwardIt2, class BinaryPred >
ForwardIt1 find_first_of( ExecutionPolicy&& policy,
                          ForwardIt1 first, ForwardIt last,
                          ForwardIt2 s_first, ForwardIt2 s_last,

                          BinaryPred p );
(4) (自 C++17 起)

在范围 [firstlast) 中搜索范围 [s_firsts_last) 中的任何元素。

1) 元素使用 operator== 进行比较。
3) 元素使用给定的二元谓词 p 进行比较。
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 起)

目录

[编辑] 参数

first, last - 定义要检查的元素范围的迭代器对
s_first, s_last - 定义要搜索的元素范围的迭代器对
policy - 要使用的执行策略
p - 二元谓词,如果元素应被视作相等则返回 ​true

谓词函数的签名应等价于以下形式

 bool pred(const Type1 &a, const Type2 &b);

虽然签名不需要带有 const &,但函数不可修改传递给它的对象,且必须能接受类型 (可能为 const) Type1Type2 的所有值,而无视值类别 (故 Type1 & 不被允许,且除非对于 Type1 移动等价于复制,否则 Type1 亦然(自 C++11 起))。
类型 Type1Type2 必须如此,使得类型为 ForwardIt1ForwardIt2 的对象可以被解引用,并随后隐式转换为 Type1Type2。 ​

类型要求
-
InputIt 必须满足 LegacyInputIterator 的要求。
-
ForwardIt 必须满足 LegacyForwardIterator 的要求。
-
ForwardIt1 必须满足 LegacyForwardIterator 的要求。
-
ForwardIt2 必须满足 LegacyForwardIterator 的要求。
-
BinaryPred 必须满足 BinaryPredicate 的要求。

[编辑] 返回值

指向范围 [firstlast) 中首个等于范围 [s_firsts_last) 中某个元素的元素的迭代器。

[s_firsts_last) 为空,或者若未找到这种元素,则返回 last

[编辑] 复杂度

Nstd::distance(first, last)Sstd::distance(s_first, s_last)

1,2) 最多 N·S 次使用 operator== 的比较。
3,4) 最多 N·S 次谓词 p 的应用。

[编辑] 异常

带有名为 ExecutionPolicy 的模板形参的重载按如下方式报告错误

  • 若作为算法一部分调用的函数的执行抛出异常,且 ExecutionPolicy标准策略之一,则调用 std::terminate。对于任何其他 ExecutionPolicy,行为是实现定义的。
  • 若算法无法分配内存,则抛出 std::bad_alloc

[编辑] 可能的实现

find_first_of (1)
template<class InputIt, class ForwardIt>
InputIt find_first_of(InputIt first, InputIt last,
                      ForwardIt s_first, ForwardIt s_last)
{
    for (; first != last; ++first)
        for (ForwardIt it = s_first; it != s_last; ++it)
            if (*first == *it)
                return first;
    return last;
}
find_first_of (3)
template<class InputIt, class ForwardIt, class BinaryPred>
InputIt find_first_of(InputIt first, InputIt last,
                      ForwardIt s_first, ForwardIt s_last,
                      BinaryPred p)
{
    for (; first != last; ++first)
        for (ForwardIt it = s_first; it != s_last; ++it)
            if (p(*first, *it))
                return first;
    return last;
}

[编辑] 示例

以下代码在整数向量中搜索任何指定的整数

#include <algorithm>
#include <iostream>
#include <vector>
 
auto print_sequence = [](const auto id, const auto& seq, int pos = -1)
{
    std::cout << id << "{ ";
    for (int i{}; auto const& e : seq)
    {
        const bool mark{i == pos};
        std::cout << (i++ ? ", " : "");
        std::cout << (mark ? "[ " : "") << e << (mark ? " ]" : "");
    }
    std::cout << " }\n";
};
 
int main()
{
    const std::vector<int> v{0, 2, 3, 25, 5};
    const auto t1 = {19, 10, 3, 4};
    const auto t2 = {1, 6, 7, 9};
 
    auto find_any_of = [](const auto& v, const auto& t)
    {
        const auto result = std::find_first_of(v.begin(), v.end(),
                                               t.begin(), t.end());
        if (result == v.end())
        {
            std::cout << "No elements of v are equal to any element of ";
            print_sequence("t = ", t);
            print_sequence("v = ", v);
        }
        else
        {
            const auto pos = std::distance(v.begin(), result);
            std::cout << "Found a match (" << *result << ") at position " << pos;
            print_sequence(", where t = ", t);
            print_sequence("v = ", v, pos);
        }
    };
 
    find_any_of(v, t1);
    find_any_of(v, t2);
}

输出

Found a match (3) at position 2, where t = { 19, 10, 3, 4 }
v = { 0, 2, [ 3 ], 25, 5 }
No elements of v are equal to any element of t = { 1, 6, 7, 9 }
v = { 0, 2, 3, 25, 5 }

[编辑] 缺陷报告

以下行为更改缺陷报告被追溯应用到先前发布的 C++ 标准。

DR 应用到 发布时的行为 正确行为
LWG 576 C++98 firstlast 需要为 LegacyForwardIterator 它们仅需为
LegacyInputIterator
LWG 1205 C++98 [s_firsts_last) 为空,则返回值不明确 在此情况下返回 last

[编辑] 参见

查找首个满足特定标准的元素
(函数模板) [编辑]
搜索元素集合中的任何一个
(算法函数对象)[编辑]