The adjacent_find function in C++ Algorithm conducts a search within the range [first, last] to locate the initial instance of two successive identical elements. When these elements are identified, the function returns an iterator pointing to the first element of the pair. In case no such elements are found, the function returns an iterator pointing to the last element in the range.
Syntax
template<class ForwardIterator>
ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last);
template<class ForwardIterator, class BinaryPredicate>
ForwardIterator adjacent_find(ForwardIterator first,ForwardIterator last BinaryPredicate pred);
Parameter
It serves as a forward iterator pointing to the initial element within the specified range.
It serves as a forward iterator pointing to the final element within the specified range.
The ```
template<class ForwardIterator>
ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last);
template<class ForwardIterator, class BinaryPredicate>
ForwardIterator adjacent_find(ForwardIterator first,ForwardIterator last BinaryPredicate pred);
## Return value
The function provides an iterator pointing to the initial element within the range [first, last) in case it detects two consecutive identical elements; otherwise, it returns the last element.
## Example 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;
}
Output:
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
## Example 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;
}
Output:
## Complexity
The function's complexity increases linearly based on the distance between the initial and final elements.
## Data races
Some or all of the components within the range are being retrieved.
## Exceptions
The function will raise an exception if any of the arguments also raises an exception.