STL algorithm学习之adjacent_find()

adjacent_find()

函数原型:

template<class _FwdIt> inline
	_FwdIt adjacent_find(_FwdIt _First, _FwdIt _Last)
	{	// find first matching successor
	_ASSIGN_FROM_BASE(_First,
		_Adjacent_find(_CHECKED_BASE(_First), _CHECKED_BASE(_Last)));
	return (_First);
	}


template<class _FwdIt,
	class _Pr> inline
	_FwdIt adjacent_find(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
	{	// find first satisfying _Pred with successor
	_ASSIGN_FROM_BASE(_First,
		_Adjacent_find(_CHECKED_BASE(_First), _CHECKED_BASE(_Last), _Pred));
	return (_First);
	}

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.
大意:找到满足条件(规则由所传函数对象决定)的第一对元素就返回,返回值为这一对里的第一个元素的迭代器(地址)


验证代码:

#include <functional>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
	string strs[] = {"123", "234", "345", "456", "456", "567", "678"};
	int    nSize  = sizeof(strs) / sizeof(string);
	string* pDest = adjacent_find( strs, strs + nSize, equal_to<string>());

	if (pDest != strs + nSize)
	{
		printf("pDest=%s index=%d\n", pDest->c_str(), pDest - strs);
	}
	else
	{
		printf("No appropriate element is found\n");
	}
	return 0;
}

运行结果:

pDest=456 index=3


你可能感兴趣的:(Algorithm,String,iterator,Class,each,pair)