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
   );

_First

A forward iterator addressing the position of the first element in the range to be searched.

             (大意)一個前向迭代器在待搜索範圍中尋找的第一個元素的地址。(開始地址)
_Last

A forward iterator addressing the position one past the final element in the range to be searched.

             (大意)一個前向迭代器在待搜索範圍中尋找最後一個元素的地址。(結束地址)(可能理解出錯,望指正)
_Comp

The binary predicate giving the condition to be satisfied by the values of the adjacent elements in the range being searched.

             (大意)二進制謂詞給出在搜索範圍內相鄰的元素的值的滿足條件。(滿足條件)

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上的示例程序:

// alg_adj_fnd.cpp : 定義控制檯應用程序的入口點。
// compile with: /EHsc

#include "stdafx.h"
#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 _tmain(int argc, _TCHAR* argv[])
{
	using namespace std;
	<span style="color:#FF0000;">list <int> L;
	list <int>::iterator Iter;
	list <int>::iterator result1, result2;</span>

       <span style="color:#FF0000;"> //push_back:在集合L後面增加一個元素</span>
	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 << <span style="color:#FF0000;">*Iter</span> <<" ";
	}
	cout << ")" <<endl;

	<span style="color:#FF0000;">result1 = adjacent_find( L.begin(), L.end() );</span>
	if( result1 == L.end() )
		cout << "There are no two adjacent elements that are equal." 
				<<endl;
	else
		cout << "There are two adjacent elements thar are equal."
				<< "\n They have a value of "
				<<*( result1 ) << "." <<endl;

	<span style="color:#FF0000;">result2 = adjacent_find( L.begin(), L.end(), twice);</span>
	if( result2 == L.end( ) )
		cout << "There are no 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;
		system("pause");
}

程序結果:



Header: <algorithm>

Namespace: std



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章