std::find, std::find_if, std::find_if_not
来自cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| 在标头 <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 搜索等于(用 operator== 比较)value 的元素。3)
find_if 搜索谓词 p 对其返回 true 的元素。5)
find_if_not 搜索谓词 q 对其返回 false 的元素。2,4,6) 同 (1,3,5),但按照
policy 执行。 这些重载只有在满足以下所有条件时才会参与重载决议:
|
|
(C++20 前) |
|
|
(C++20 起) |
参数
| first, last | - | 要检验的元素范围的迭代器对 |
| value | - | 要与元素比较的值 |
| policy | - | 所用的执行策略 |
| p | - | 如果是要求的元素则返回 true 的一元谓词。对每个(可为 const 的) |
| q | - | 如果是要求的元素则返回 false 的一元谓词。对每个(可为 const 的) |
| 类型要求 | ||
-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 次
operator== 与 value 进行比较。3,4) 最多应用 N 次谓词
p。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 不包含 " << 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) |
查找首个满足特定条件的元素 (算法函数对象) |