find_if函数

出处: http://msdn.microsoft.com/zh-cn/library/wkwky1wa 

Locates the position of the first occurrence of an element in a range that satisfies a specified condition.

template<class InputIterator, class Predicate>
   InputIterator find_if(
      InputIterator _First, 
      InputIterator _Last, 
      Predicate _Pred
   );
_First

An input iterator addressing the position of the first element in the range to be searched.

_Last

An input iterator addressing the position one past the final element in the range to be searched.

_Pred

User-defined predicate function object that defines the condition to be satisfied by the element being searched for. A predicate takes single argument and returns true or false.

An input iterator that addresses the first element in the range that satisfies the condition specified by the predicate.

This template function is a generalization of the algorithm find, replacing the predicate "equals a specific value" with any predicate.

// 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;
   }
}
L = ( 5 10 15 20 10 )
There is an element greater than 10 in list L,
 and it is followed by a 20.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值