次の方法で共有


find_if

指定された条件を満たす範囲の要素の最初に出現する位置を検索します。

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

パラメーター

  • _First
    検索する範囲の先頭の要素の位置を示す入力反復子。

  • _Last
    検索する範囲の最後の要素 1 を超える位置を示す入力反復子。

  • _Pred
    検索する要素が満たされた要件を定義するユーザー定義の述語関数オブジェクト。述語は、一つの引数を受け取り、truefalseを返します。

戻り値

条件を満たす範囲内の先頭の要素を示す入力反復子は、述語によって指定されます。

解説

このテンプレート関数は、述語に述語 「等しい」特定の値を置き換えるアルゴリズム 検索の原則です。

使用例

// 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

参照

関連項目

find_if (STL Samples)

標準テンプレート ライブラリ