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

本文介绍了C++标准库中的adjacent_find()函数,详细解释了如何使用此函数来查找相邻且相等或满足特定条件的元素对。通过示例演示了如何利用字符串数组查找重复项。

628

被折叠的 条评论
为什么被折叠?



