std:: find, std:: find_if, std:: find_if_not
|
定义于头文件
<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
>
|
(C++26 起) | |
| (2) | ||
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
T
>
ForwardIt find
(
ExecutionPolicy
&&
policy,
|
(C++17 起)
(C++26 前) |
|
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
T
=
typename
std::
iterator_traits
|
(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,
|
(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,
|
(6) | (C++17 起) |
返回指向范围
[
first
,
last
)
中首个满足特定条件的元素的迭代器(若不存在这样的迭代器则返回
last
)。
find
搜索与
value
相等的元素(使用
operator==
)。
find_if
搜索使谓词
p
返回
true
的元素。
find_if_not
查找使谓词
q
返回
false
的元素。
|
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
,其中参数
|
| q | - |
对所需元素返回
false
的一元谓词
表达式
q
(
v
)
必须可转换为
bool
,其中参数
|
| 类型要求 | ||
-
InputIt
必须满足
LegacyInputIterator
的要求。
|
||
-
ForwardIt
必须满足
LegacyForwardIterator
的要求。
|
||
-
UnaryPredicate
必须满足
Predicate
的要求。
|
||
返回值
范围
[
first
,
last
)
中首个满足以下条件的迭代器
it
,若不存在此类迭代器则返回
last
:
复杂度
给定 N 为 std:: distance ( first, last ) :
operator==
)。
异常
带有名为
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
|
移除了该要求 |
参见
|
查找首个相邻的相等元素(或满足给定谓词的元素)
(函数模板) |
|
|
在特定范围内查找最后出现的元素序列
(函数模板) |
|
|
搜索一组元素中的任意一个元素
(函数模板) |
|
|
查找两个范围首次出现差异的位置
(函数模板) |
|
|
搜索元素范围的首次出现
(函数模板) |
|
|
(C++20)
(C++20)
(C++20)
|
查找满足特定条件的首个元素
(算法函数对象) |