adjacent_find

Equals または指定された条件を満たす 2 個の隣接する要素を検索します。

template<class ForwardIterator>
   ForwardIterator adjacent_find(
      ForwardIterator _First, 
      ForwardIterator _Last
   );
template<class ForwardIterator , class BinaryPredicate>
   ForwardIterator adjacent_find(
      ForwardIterator _First, 
      ForwardIterator _Last, 
      BinaryPredicate _Comp
   );

パラメーター

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

  • _Last
    検索する範囲の最後の要素の一つ前の位置 1 に対処前方反復子。

  • _Comp
    検索範囲の隣接する要素の値が満たされた要件を与えるバイナリ述語。

戻り値

このようなペアの要素がの場合は、互いに等しいか、そのバイナリ述語によって指定された条件を満たす隣接する二つの最初の要素への前方反復子 (最初のバージョンでは) (2 番目の形式で)。それ以外 _Last を指す反復子を返します。

解説

adjacent_find のアルゴリズムは nonmutating シーケンスのアルゴリズムです。検索する範囲は有効である必要があります; すべてのポインターが dereferenceable なり、最後の位置は incrementation によって最初からアクセスできます。アルゴリズムの時間の複雑さがスコープに含まれる要素の数で直線的です。

要素間の一致項目を決定するために使用される operator== がオペランド間の等価関係を課さなければ必要があります。

使用例

// alg_adj_fnd.cpp
// compile with: /EHsc
#include <list>
#include <algorithm>
#include <iostream>

// Returns whether second element is twice the first
bool twice (int elem1, int elem2 )
{
   return elem1 * 2 == elem2;
}

int main( ) 
{
   using namespace std;
   list <int> L;
   list <int>::iterator Iter;
   list <int>::iterator result1, result2;
   
   L.push_back( 50 );
   L.push_back( 40 );
   L.push_back( 10 );
   L.push_back( 20 );
   L.push_back( 20 );

   cout << "L = ( " ;
   for ( Iter = L.begin( ) ; Iter != L.end( ) ; Iter++ )
      cout << *Iter << " ";
   cout << ")" << endl;
   
   result1 = adjacent_find( L.begin( ), L.end( ) );
   if ( result1 == L.end( ) )
      cout << "There are not two adjacent elements that are equal."
           << endl;
   else
      cout << "There are two adjacent elements that are equal."
           << "\n They have a value of "
           <<  *( result1 ) << "." << endl;

   result2 = adjacent_find( L.begin( ), L.end( ), twice );
   if ( result2 == L.end( ) )
      cout << "There are not two adjacent elements where the "
           << " second is twice the first." << endl;
   else
      cout << "There are two adjacent elements where "
           << "the second is twice the first."
           << "\n They have values of " << *(result2++);
      cout << " & " << *result2 << "." << endl;
}
  
  
  
  

必要条件

ヘッダー: <algorithm>

名前空間: std

参照

関連項目

Nonpredicate Version of adjacent_find

Predicate Version of adjacent_find

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