std::find, std::find_if, std::find_if_not
来自 zh.cppreference.net
| 定义于头文件 <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搜索第一个等于 value(使用 operator==)。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 对于所需元素。 表达式 |
| q | - | 一元谓词,返回 false 对于所需元素。 表达式 |
| policy | - | 指 执行策略 使用 |
| 类型要求 | ||
-InputIt 必须满足 LegacyInputIterator。
| ||
-ForwardIt 必须满足 LegacyForwardIterator。
| ||
-UnaryPredicate 必须满足 Predicate。
| ||
返回值
第一个迭代器 it 在源范围内满足以下条件或 last 如果没有这样的迭代器:
1,4)
*it == value 是 true。2,5)
p(*it) 是 true。3,6)
q(*it) 是 false。复杂度
给定 N 作为 std::distance(first, last):
1) 最多 N 次比较通过
value 使用 operator==。2) 最多 N 次应用
p。3) 最多 N 次应用
q。4) 𝓞(N) 次比较通过
value 使用 operator==。5) 𝓞(N) 次应用
p。6) 𝓞(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));
}
|
| 特性测试 宏 | 值 | Std | 特性 |
|---|---|---|---|
__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 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++ 标准。
| DR | 应用于 | 已发布的行为 | 正确行为 |
|---|---|---|---|
| LWG 283 | C++98 | T 要求 EqualityComparable,但的值类型 InputIt 可能不是 T
|
移除了该要求 |
参阅
(C++20)(C++20)(C++20) |
寻找第一个满足特定条件的元素 (算法函数对象) |
| 寻找第一对相等的相邻元素(或满足给定谓词的元素) (函数模板 & 算法函数对象) | |
(C++20) |
|
| 在某个范围内寻找最后一组元素 (函数模板 & 算法函数对象) | |
(C++20) |
|
| 搜索任一元素集合中的任何一个 (函数模板 & 算法函数对象) | |
(C++20) |
|
| 寻找两个范围第一个不同的位置 (函数模板 & 算法函数对象) | |
(C++20) |
|
| 搜索第一个元素的范围出现 (函数模板 & 算法函数对象) | |
(C++20) |