Algorithm学习之adjacent_find学习
从MSDN下查阅得到:
adjacent_find
Visual Studio 2010
Searches for two adjacent elements that are either equal or satisfy a specified condition.
找到两个相邻的元素,这两个相邻的元素或者相等或者满足特定的条件。
template<class ForwardIterator> ForwardIterator adjacent_find( ForwardIterator _First, ForwardIterator _Last ); template<class ForwardIterator , class BinaryPredicate> ForwardIterator adjacent_find( ForwardIterator _First, ForwardIterator _Last, BinaryPredicate _Comp );
Return Value
A forward iterator to the first element of the adjacent pair that are either equal to each other (in the first version) or that satisfy the condition given by the binary predicate (in the second version), provided that such a pair of elements is found. Otherwise, an iterator pointing to _Last is returned.大意:找到满足条件(规则由所传函数对象决定)的第一对元素就返回,返回值为这一对里的第一个元素的迭代器(地址)。否则返回最后一个元素的指针地址。
The adjacent_find algorithm is a nonmutating sequence algorithm. The range to be searched must be valid; all pointers must be dereferenceable and the last position is reachable from the first by incrementation. The time complexity of the algorithm is linear in the number of elements contained in the range.
这个相邻_查找算法是一个非变形的序列化算法。搜索范围必须是有效的;所有的指针必须是可以引用的并且最后一个位置必须能够由第一个位置自增长得到。时间复杂度和范围内包含的元素的个数成线性关系。
The operator== used to determine the match between elements must impose an equivalence relation between its operands.
操作符 == 被用来决定元素之间的匹配关系,== 必须表示操作数间的等价关系。
MSDN上的示例程序:
1 // alg_adj_fnd.cpp : 定义控制台应用程序的入口点。 2 // compile with: /EHsc 3 4 #include "stdafx.h" 5 #include <list> 6 #include <algorithm> 7 #include <iostream> 8 9 //Returns whether second element is twice the first 10 //返回第二个元素是否是第一个元素的两倍 11 bool twice (int elem1, int elem2 ) 12 { 13 return elem1 * 2 == elem2; 14 } 15 16 int _tmain(int argc, _TCHAR* argv[]) 17 { 18 using namespace std; 19 <span style="color:#FF0000;">list <int> L; 20 list <int>::iterator Iter; 21 list <int>::iterator result1, result2;</span> 22 23 //push_back:在集合L后面增加一个元素 24 L.push_back(50); 25 L.push_back(40); 26 L.push_back(10); 27 L.push_back(20); 28 L.push_back(20); 29 30 cout << "L = ( "; 31 for( Iter = L.begin( ) ; Iter != L.end( ) ; Iter++ ) 32 { 33 cout << <span style="color:#FF0000;">*Iter</span> <<" "; 34 } 35 cout << ")" <<endl; 36 37 <span style="color:#FF0000;">result1 = adjacent_find( L.begin(), L.end() );</span> 38 if( result1 == L.end() ) 39 cout << "There are no two adjacent elements that are equal." 40 <<endl; 41 else 42 cout << "There are two adjacent elements thar are equal." 43 << "\n They have a value of " 44 <<*( result1 ) << "." <<endl; 45 46 <span style="color:#FF0000;">result2 = adjacent_find( L.begin(), L.end(), twice);</span> 47 if( result2 == L.end( ) ) 48 cout << "There are no two adjacent elements where the " 49 << " second is twice the first." << endl; 50 else 51 cout << "There are two adjacent elements where " 52 << "the second is twice the first." 53 <<"\n They have values of " << *(result2++); 54 cout << " & " << *result2 << "." <<endl; 55 system("pause"); 56 }
程序结果:
Header: <algorithm>
Namespace: std