STL algorithm学习之adjacent_find()

2 篇文章 0 订阅
2 篇文章 0 订阅
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


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值