std::find_if
STD:找到,STD::找到[医]如果,STD::找到[医]如果[医]不
Defined in header | | |
---|---|---|
template< class InputIt, class T > InputIt find( InputIt first, InputIt last, const T& value | (1) | |
template< class ExecutionPolicy, class ForwardIt, class T > ForwardIt find( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, const T& value | (2) | (since C++17) |
template< class InputIt, class UnaryPredicate > InputIt find_if( InputIt first, InputIt last, UnaryPredicate p | (3) | |
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate > ForwardIt find_if( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate p | (4) | (since C++17) |
template< class InputIt, class UnaryPredicate > InputIt find_if_not( InputIt first, InputIt last, UnaryPredicate q | (5) | (since C++11) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate > ForwardIt find_if_not( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate q | (6) | (since C++17) |
返回范围中的第一个元素。[first, last)
满足具体标准:
1%29find
搜索等于value
3%29find_if
搜索谓词为其的元素。p
回报true
5%29find_if_not
搜索谓词为其的元素。q
回报false
2,4,6%29等于%281,3,5%29,但根据policy。此重载只参与以下情况下的过载解决方案:std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的
参数
first, last | - | the range of elements to examine |
---|---|---|
value | - | value to compare the elements to |
policy | - | the execution policy to use. See execution policy for details. |
p | - | unary predicate which returns true for the required element. The signature of the predicate function should be equivalent to the following: bool pred(const Type &a The signature does not need to have const &, but the function must not modify the objects passed to it. The type Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type. |
q | - | unary predicate which returns false for the required element. The signature of the predicate function should be equivalent to the following: bool pred(const Type &a The signature does not need to have const &, but the function must not modify the objects passed to it. The type Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type. |
类型要求
-输入必须符合输入器的要求。
---。
-单数预测必须满足谓词的要求。
返回值
满足条件的第一个元素的迭代器last
如果找不到这样的元素。
复杂性
顶多last
---first
谓词的应用。
例外
带有名为ExecutionPolicy
报告错误如下:
- 如果执行作为算法一部分调用的函数,则引发异常
ExecutionPolicy
是其中之一标准政策,,,std::terminate
叫做。对于任何其他人ExecutionPolicy
,行为是由实现定义的。
- 如果算法不能分配内存,
std::bad_alloc
被扔了。
可能的实施
第一版
*。
模板<class Inputit,class T>InputIt查找%28 InputIt First,Inputit Lest,Const T&value%29{表示%28;First%21=Lest;++First%29{if%28%2A首先==值%29{先返回;}返回最后;}
第二版
模板<类Inputit,类UnaryPredicate>InputIt找到[医]如果%28 InputIt First,Inputit Lest,则UnaryPredicate p%29{表示%28;First%21=Lest;++First%29{if%28p%28%2A第一%29%29{先返回;}最后返回;}
第三版
模板<类Inputit,类UnaryPredicate>InputIt找到[医]如果[医]不是%28输入第一,输入是最后,单预言Q%29{表示%28;第一%21=最后;++第29%{如果%28%21 q%28%2A第一%29%29{先返回;}最后返回;}
如果没有C++11,则相当于std::find_if_not
是使用std::find_if
用否定的谓词。
模板<类Inputit,类UnaryPredicate>InputIt找到[医]如果[医]不是%28 InputIt First,InputIt Lest,UnaryPredicate Q%29{返回std::find[医]如果%281,最后,std::Not 1%28q%29%29;}
*。
例
下面的示例在整数向量中查找整数。
二次
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
int main()
{
int n1 = 3;
int n2 = 5;
std::vector<int> v{0, 1, 2, 3, 4};
auto result1 = std::find(std::begin(v), std::end(v), n1
auto result2 = std::find(std::begin(v), std::end(v), n2
if (result1 != std::end(v)) {
std::cout << "v contains: " << n1 << '\n';
} else {
std::cout << "v does not contain: " << n1 << '\n';
}
if (result2 != std::end(v)) {
std::cout << "v contains: " << n2 << '\n';
} else {
std::cout << "v does not contain: " << n2 << '\n';
}
}
二次
产出:
二次
v contains: 3
v does not contain: 5
二次
另见
adjacent_find | finds the first two adjacent items that are equal (or satisfy a given predicate) (function template) |
---|---|
find_end | finds the last sequence of elements in a certain range (function template) |
find_first_of | searches for any one of a set of elements (function template) |
mismatch | finds the first position where two ranges differ (function template) |
search | searches for a range of elements (function template) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。