来自cppreference.com
| 在标头 <algorithm> 定义
|
||
template< class InputIt, class T >
InputIt find( InputIt first, InputIt last, const T& value );
|
(1) | (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 起) | |
template< class InputIt, class UnaryPred >
InputIt find_if( InputIt first, InputIt last, UnaryPred p );
|
(2) | (C++20 起为 constexpr) |
template< class InputIt, class UnaryPred >
InputIt find_if_not( InputIt first, InputIt last, UnaryPred q );
|
(3) | (C++11 起) (C++20 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt, class T >
ForwardIt find( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, const T& value );
|
(4) | (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 ExecutionPolicy, class ForwardIt, class UnaryPred >
ForwardIt find_if( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, UnaryPred p );
|
(5) | (C++17 起) |
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 搜索等于(用 operator== 比较)value 的首个元素。2)
find_if 搜索谓词 p 对其返回 true 的首个元素。3)
find_if_not 搜索谓词 q 对其返回 false 的首个元素。4-6) 同 (1-3),但按照
policy 执行。 这些重载只有在以下表达式的值是
true 时才会参与重载决议:
|
|
(C++20 前) |
|
|
(C++20 起) |
参数
| first, last | - | 表示源范围的迭代器对 |
| value | - | 要与元素比较的值 |
| p | - | 如果是要求的元素则返回 true 的一元谓词。对每个(可为 const 的) |
| q | - | 如果是要求的元素则返回 false 的一元谓词。对每个(可为 const 的) |
| policy | - | 所用的执行策略 |
| 类型要求 | ||
-InputIt 必须满足老式输入迭代器 (LegacyInputIterator) 。
| ||
-ForwardIt 必须满足老式向前迭代器 (LegacyForwardIterator) 。
| ||
-UnaryPredicate 必须满足谓词 (Predicate) 。
| ||
返回值
源范围中首个满足以下条件的迭代器 it,或者在没有满足条件的迭代器时返回 last:
1,4)
*it == value 是 true。2,5)
p(*it) 是 true。3,6)
q(*it) 是 false。复杂度
给定 \(\scriptsize N\)N 为 std::distance(first, last):
1) 最多应用 \(\scriptsize N\)N 次
operator== 与 value 进行比较。2) 最多应用 \(\scriptsize N\)N 次
p。3) 最多应用 \(\scriptsize N\)N 次
q。4) 应用 \(\scriptsize \mathcal{O}(N)\)𝓞(N) 次
operator== 与 value 进行比较。5) 应用 \(\scriptsize \mathcal{O}(N)\)𝓞(N) 次
p。6) 应用 \(\scriptsize \mathcal{O}(N)\)𝓞(N) 次
q。异常
4-6) 在执行过程中:
- 如果并行化所需的临时内存资源不可用,那么就会抛出 std::bad_alloc。
- 如果在通过算法实参访问对象时抛出了未捕获的异常,那么行为由执行策略决定(标准策略会调用 std::terminate)。
可能的实现
| find |
|---|
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 |
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));
}
|
| 功能特性测试宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__cpp_lib_algorithm_default_value_type |
202403 |
(C++26) | 算法中的列表初始化 (1,4) |
示例
以下示例在给定的序列中查找数值。
运行此代码
#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 不包含 " << needle << '\n';
else
std::cout << "haystack 包含 " << 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 包含偶数:" << *it << '\n';
else
std::cout << "haystack 不包含偶数\n";
}
}
void example_list_init()
{
std::vector<std::complex<double>> haystack{{4.0, 2.0}};
#ifdef __cpp_lib_algorithm_default_value_type
// 推导的 T 使得列表初始化成为可能
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 包含 3
haystack 不包含 5
haystack 包含偶数:4
haystack 不包含偶数
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 283 | C++98 | T 需要是可相等比较 (EqualityComparable) 的,但是 InputIt 的值类型不一定是 T
|
移除该要求 |
参阅
(C++20)(C++20)(C++20) |
查找首个满足特定条件的元素 (算法函数对象) |
| 查找首对相同(或满足给定谓词)的相邻元素 (函数模板 & 算法函数对象) | |
(C++20) |
|
| 查找元素序列在特定范围中最后一次出现 (函数模板 & 算法函数对象) | |
(C++20) |
|
| 搜索一组元素中任一元素 (函数模板 & 算法函数对象) | |
(C++20) |
|
| 查找两个范围的首个不同之处 (函数模板 & 算法函数对象) | |
(C++20) |
|
| 搜索元素范围的首次出现 (函数模板 & 算法函数对象) | |
(C++20) |