Namespaces
Variants

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

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>
(1)
template < class InputIt, class T >
InputIt find ( InputIt first, InputIt last, const T & value ) ;
(C++20 起为 constexpr)
(C++26 前)
template < class InputIt, class T = typename std:: iterator_traits

< InputIt > :: value_type >

constexpr InputIt find ( InputIt first, InputIt last, const T & value ) ;
(C++26 起)
(2)
template < class ExecutionPolicy, class ForwardIt, class T >

ForwardIt find ( ExecutionPolicy && policy,

ForwardIt first, ForwardIt last, const T & value ) ;
(C++17 起)
(C++26 前)
template < class ExecutionPolicy,

class ForwardIt, class T = typename std:: iterator_traits
< ForwardIt > :: value_type >
ForwardIt find ( ExecutionPolicy && policy,

ForwardIt first, ForwardIt last, const T & value ) ;
(C++26 起)
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 起)

返回指向范围 [ first , last ) 中首个满足特定条件的元素的迭代器(若不存在这样的迭代器则返回 last )。

1) find 搜索与 value 相等的元素(使用 operator== )。
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 >> true

(C++20 前)

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

(C++20 起)

目录

参数

first, last - 定义待检查元素范围的迭代器对
value - 用于与元素比较的值
policy - 使用的执行策略
p - 对所需元素返回 ​ true 的一元谓词

表达式 p ( v ) 必须可转换为 bool ,其中参数 v 为(可能 const 限定的) VT 类型, VT InputIt 的值类型,且与值类别无关。该表达式不得修改 v 。因此不允许使用 VT & 参数类型 ,也不允许使用 VT 类型,除非对于 VT 移动操作等价于拷贝操作 (since C++11) 。 ​

q - 对所需元素返回 ​ false 的一元谓词

表达式 q ( v ) 必须可转换为 bool ,其中参数 v 为(可能 const 限定的) VT 类型, VT InputIt 的值类型,且与值类别无关。该表达式不得修改 v 。因此不允许使用 VT & 参数类型 ,也不允许使用 VT 类型,除非对于 VT 移动操作等价于拷贝操作 (since C++11) 。 ​

类型要求
-
InputIt 必须满足 LegacyInputIterator 的要求。
-
ForwardIt 必须满足 LegacyForwardIterator 的要求。
-
UnaryPredicate 必须满足 Predicate 的要求。

返回值

范围 [ first , last ) 中首个满足以下条件的迭代器 it ,若不存在此类迭代器则返回 last

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

复杂度

给定 N std:: distance ( first, last )

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

异常

带有名为 ExecutionPolicy 模板参数的重载按以下方式报告错误:

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

可能的实现

find (1)
template<class InputIt, class T = typename std::iterator_traits<InputIt>::value_type>
constexpr InputIt find(InputIt first, InputIt last, const T& value)
{
    for (; first != last; ++first)
        if (*first == value)
            return first;
    return last;
}
find_if (3)
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 (5)
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));
}
功能测试 标准 功能
__cpp_lib_algorithm_default_value_type 202403 (C++26) 列表初始化 用于算法 ( 1,2 )

示例

以下示例在给定序列中查找数字。

#include <algorithm>
#include <array>
#include <cassert>
#include <complex>
#include <initializer_list>
#include <iostream>
#include <vector>
bool is_even(int i)
{
    return i % 2 == 0;
}
void example_contains()
{
    const auto haystack = {1, 2, 3, 4};
    for (const int needle : {3, 5})
        if (std::find(haystack.begin(), haystack.end(), needle) == haystack.end())
            std::cout << "haystack does not contain " << needle << '\n';
        else
            std::cout << "haystack contains " << needle << '\n';
}
void example_predicate()
{
    for (const auto& haystack : {std::array{3, 1, 4}, {1, 3, 5}})
    {
        const auto it = std::find_if(haystack.begin(), haystack.end(), is_even);
        if (it != haystack.end())
            std::cout << "haystack contains an even number " << *it << '\n';
        else
            std::cout << "haystack does not contain even numbers\n";
    }
}
void example_list_init()
{
    std::vector<std::complex<double>> haystack{{4.0, 2.0}};
#ifdef __cpp_lib_algorithm_default_value_type
    // T gets deduced making list-initialization possible
    const auto it = std::find(haystack.begin(), haystack.end(), {4.0, 2.0});
#else
    const auto it = std::find(haystack.begin(), haystack.end(), std::complex{4.0, 2.0});
#endif
    assert(it == haystack.begin());  
}
int main()
{
    example_contains();
    example_predicate();
    example_list_init();
}

输出:

haystack contains 3
haystack does not contain 5
haystack contains an even number 4
haystack does not contain even numbers

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用范围 发布时行为 正确行为
LWG 283 C++98 T 被要求满足 EqualityComparable 要求,但
InputIt 的值类型可能不是 T
移除了该要求

参见

查找首个相邻的相等元素(或满足给定谓词的元素)
(函数模板)
在特定范围内查找最后出现的元素序列
(函数模板)
搜索一组元素中的任意一个元素
(函数模板)
查找两个范围首次出现差异的位置
(函数模板)
搜索元素范围的首次出现
(函数模板)
查找满足特定条件的首个元素
(算法函数对象)