find_if
找到元素的第一次出现的位置满足指定条件的大小。
template<class InputIterator, class Predicate>
InputIterator find_if(
InputIterator _First,
InputIterator _Last,
Predicate _Pred
);
参数
_First
解决输入的迭代器第一个元素的位置在要搜索的范围。_Last
解决输入的迭代器通过最终元素的位置一在要搜索的范围。_Pred
定义元素将满足的条件要搜索的用户定义的谓词函数对象。 谓词采用单个参数并返回 true 或 false。
返回值
解决在范围内的第一个元素满足条件的输入迭代器谓词所指定的。
备注
此模板函数是算法 查找的泛化,替换谓词“等于一个特定值”的所有谓词。
示例
// alg_find_if.cpp
// compile with: /EHsc
#include <list>
#include <algorithm>
#include <iostream>
bool greater10 ( int value )
{
return value >10;
}
int main( )
{
using namespace std;
list <int> L;
list <int>::iterator Iter;
list <int>::iterator result;
L.push_back( 5 );
L.push_back( 10 );
L.push_back( 15 );
L.push_back( 20 );
L.push_back( 10 );
cout << "L = ( " ;
for ( Iter = L.begin( ) ; Iter != L.end( ) ; Iter++ )
cout << *Iter << " ";
cout << ")" << endl;
result = find_if( L.begin( ), L.end( ), &greater10 );
if ( result == L.end( ) )
cout << "There is no element greater than 10 in list L."
<< endl;
else
{
result++;
cout << "There is an element greater than 10 in list L,"
<< "\n and it is followed by a "
<< *(result) << "." << endl;
}
}
要求
标头: <algorithm>
命名空间: std