C++ neighbor_find()
 
 
  C++ 算法neighbor_find()函数对两个连续匹配元素的首次出现在[first,last]范围内执行搜索操作。如果找到这样的元素,则返回两个元素的第一个元素的迭代器。否则,将返回last元素。
 
语法
 
 
 
  template<class ForwardIterator>
ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last);
template<class ForwardIterator, class BinaryPredicate>
ForwardIterator adjacent_find(ForwardIterator first,ForwardIterator last BinaryPredicate pred);
 
   
  
参数
 
  first: 它是该范围内第一个元素的正向迭代器。
 
  last: 它是范围中last元素的正向迭代器。
 
  pred: 它是一个二进制函数,接受两个元素作为参数并执行设计的任务
 
返回值
 
 如果找到两个连续的匹配元素,则该函数将迭代器返回到range [first,last)的第一个元素返回last元素。
 
示例1 
 
 
 
  #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool myfunc(int j, int k)
{
    return(j==k);
}
int main()
{
    int newints[]={5,20,5,50,50,20,60,60,20};
    std::vector<int> newvector(newints, newints+8);
    std::vector<int>::iterator ti;
    ti=std::adjacent_find(newvector.begin(),newvector.end());
    if(ti!=newvector.end())
    std::cout<<"In the given range the first pair of sequence that is repeated is:"<<*ti<<"\n";
    ti=std::adjacent_find(++ti,newvector.end(),myfunc);
    if(ti!=newvector.end())
    std::cout<<"In the given range the second pair of sequence  that is repeated is:"<<*ti<<"\n";
    return 0;
} 
   
  
  输出: 
 
 
 
  In the given range the first pair of sequence that is repeated are: 50
In the given range the second pair of sequence that is repeated are: 60
 
   
  
示例2 
 
 
 
  #include<iostream>
#include<algorithm>
int main()
{
    int A[]={12,14,17,17,19};
    int n=sizeof(A)/sizeof(A[0]);
    int* ti=std::adjacent_find(A,A+n);
    std::cout<<*ti;
} 
   
  
  输出: 
 
 
复杂度
 
 该函数的复杂度与第一个元素和last元素之间的距离呈线性关系。
 
数据竞争
 
 可以访问范围中的某些或所有元素。
 
异常
 
 如果任何参数抛出一个异常,该函数就会引发异常。