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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值