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 算法是一个nonmutating的顺序算法。 要搜索的范围必须是有效的;所有指针必须dereferenceable,最后一个位置开始可访问按增量。 算法的时间复杂是线性范围内包含的元素的数目。
用于的 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