std::find, std::find_if, std::find_if_not

来自cppreference.com
< cpp‎ | algorithm
 
 
算法库
受约束算法及范围上的算法 (C++20)
包含算法例如 ranges::copy, ranges::sort, ...
执行策略 (C++17)
排序和相关操作
划分操作
排序操作
二分搜索操作(在已划分范围上)
集合操作(在有序范围上)
归并操作(在有序范围上)
堆操作
最小/最大操作
(C++11)
(C++17)
字典序比较操作
排列操作
C 库

数值运算
(C++11)                       
在未初始化内存上的操作
 
在标头 <algorithm> 定义
template< class InputIt, class T >
InputIt find( InputIt first, InputIt last, const T& value );
(1) (C++20 起 constexpr)
template< class ExecutionPolicy, class ForwardIt, class T >

ForwardIt find( ExecutionPolicy&& policy,

                ForwardIt first, ForwardIt last, const T& value );
(2) (C++17 起)
template< class InputIt, class UnaryPred >
InputIt find_if( InputIt first, InputIt last, UnaryPred p );
(3) (C++20 起 constexpr)
template< class ExecutionPolicy, class ForwardIt, class UnaryPred >

ForwardIt find_if( ExecutionPolicy&& policy,

                   ForwardIt first, ForwardIt last, UnaryPred p );
(4) (C++17 起)
template< class InputIt, class UnaryPred >
InputIt find_if_not( InputIt first, InputIt last, UnaryPred q );
(5) (C++11 起)
(C++20 起 constexpr)
template< class ExecutionPolicy, class ForwardIt, class UnaryPred >

ForwardIt find_if_not( ExecutionPolicy&& policy,

                       ForwardIt first, ForwardIt last, UnaryPred q );
(6) (C++17 起)

返回指向范围 [firstlast) 中满足特定判别标准的首个元素的迭代器(没有这种元素时返回 last)。

1) find 搜索等于(用 operator== 比较)value 的元素。
3) find_if 搜索谓词 p 对其返回 true 的元素。
5) find_if_not 搜索谓词 q 对其返回 false 的元素。
2,4,6)(1,3,5),但按照 policy 执行。
这些重载只有在

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

(C++20 前)

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

(C++20 起)
true 时时才会参与重载决议。

目录

[编辑] 参数

first, last - 要检验的元素范围
value - 要与元素比较的值
policy - 所用的执行策略。细节见执行策略
p - 如果是要求的元素则返回 ​true 的一元谓词。

对每个(可为 const 的) VT 类型参数 v ,其中 VTInputIt 的值类型,表达式 p(v) 必须可转换到 bool,无关乎值类别,而且必须不修改 v 。从而不允许 VT& 类型参数,亦不允许 VT ,除非对 VT 而言移动等价于复制 (C++11 起)。 ​

q - 如果是要求的元素则返回 ​false 的一元谓词。

对每个(可为 const 的) VT 类型参数 v ,其中 VTInputIt 的值类型,表达式 q(v) 必须可转换到 bool,无关乎值类别,而且必须不修改 v 。从而不允许 VT& 类型参数,亦不允许 VT ,除非对 VT 而言移动等价于复制 (C++11 起)。 ​

类型要求
-
InputIt 必须满足老式输入迭代器 (LegacyInputIterator)
-
ForwardIt 必须满足老式向前迭代器 (LegacyForwardIterator)
-
UnaryPredicate 必须满足谓词 (Predicate)

[编辑] 返回值

范围 [firstlast) 中首个满足以下条件的迭代器 it,或者在没有满足条件的迭代器时返回 last

1,2) *it == valuetrue
3,4) p(*it)true
5,6) q(*it)false

[编辑] 复杂度

给定 Nstd::distance(first, last)

1,2) 最多应用 Noperator==value 进行比较。
3,4) 最多应用 N 次谓词 p
5,6) 最多应用 N 次谓词 q

[编辑] 异常

拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:

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

[编辑] 可能的实现

find
template<class InputIt, class T>
constexpr InputIt find(InputIt first, InputIt last, const T& value)
{
    for (; first != last; ++first)
        if (*first == value)
            return first;
 
    return last;
}
find_if
template<class InputIt, class UnaryPred>
constexpr InputIt find_if(InputIt first, InputIt last, UnaryPred p)
{
    for (; first != last; ++first)
        if (p(*first))
            return first;
 
    return last;
}
find_if_not
template<class InputIt, class UnaryPred>
constexpr InputIt find_if_not(InputIt first, InputIt last, UnaryPred q)
{
    for (; first != last; ++first)
        if (!q(*first))
            return first;
 
    return last;
}

[编辑] 注解

如果没有 C++11,那么 std::find_if_not 的等价版本是以取反的谓词使用 std::find_if

template<class InputIt, class UnaryPred>
InputIt find_if_not(InputIt first, InputIt last, UnaryPred q)
{
    return std::find_if(first, last, std::not1(q));
}

[编辑] 示例

以下示例在给定的 std::vector 中查找整数。

#include <algorithm>
#include <array>
#include <iostream>
 
int main()
{
    const auto v = {1, 2, 3, 4};
 
    for (const int n : {3, 5})
        (std::find(v.begin(), v.end(), n) == std::end(v))
            ? std::cout << "v 不包含 " << n << '\n'
            : std::cout << "v 包含 " << n << '\n';
 
    auto is_even = [](int i) { return i % 2 == 0; };
 
    for (auto const& w : {std::array{3, 1, 4}, {1, 3, 5}})
        if (auto it = std::find_if(begin(w), end(w), is_even); it != std::end(w))
            std::cout << "w 包含偶数:" << *it << '\n';
        else
            std::cout << "w 不包含偶数\n";
}

输出:

v 包含 3
v 不包含 5
w 包含偶数:4
w 不包含偶数

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 出版时的行为 正确行为
LWG 283 C++98 T 需要是可相等比较 (EqualityComparable) 的,但是 InputIt 的值类型不一定是 T 移除该要求

[编辑] 参阅

查找首对相邻的相同(或满足给定谓词的)元素
(函数模板) [编辑]
在特定范围中寻找最后出现的元素序列
(函数模板) [编辑]
搜索元素集合中的任意元素
(函数模板) [编辑]
寻找两个范围出现不同的首个位置
(函数模板) [编辑]
搜索一个元素范围
(函数模板) [编辑]
查找满足特定条件的的第一个元素
(niebloid) [编辑]