adjacent_find
같지 않거나 지정한 조건을 충족하는 인접 요소 두 개를 검색합니다.
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
검색할 범위에서 마지막 요소 하나 다음의 위치 주소를 지정하는 정방향 반복기입니다._Comp
검색 중인 범위 내 인접 요소의 값이 충족하도록 할 조건을 제공하는 이진 조건자입니다.
반환 값
서로 같거나(첫 번째 버전) 이진 조건자가 지정한 조건을 충족하는(두 번째 버전) 인접 쌍의 첫 번째 요소에 대한 정방향 반복기입니다(해당 요소 쌍이 있는 경우). 그렇지 않으면 _Last를 가리키는 반복기가 반환됩니다.
설명
adjacent_find 알고리즘은 변경할 수 없는 시퀀스 알고리즘입니다. 검색할 범위는 유효해야 하고 모든 포인터는 역참조 가능해야 하며 처음 위치에서 증분하여 마지막 위치까지 도달할 수 있어야 합니다. 알고리즘의 시간 복잡도는 범위에 포함된 요소 수에 비례합니다.
요소 간의 일치를 확인하는 데 사용되는 **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