Algorithm Adjacent Find Function - C++ Programming Tutorial
C++ Course / STL Algorithm / Algorithm Adjacent Find Function

Algorithm Adjacent Find Function

BLUF: Mastering Algorithm Adjacent Find Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Algorithm Adjacent Find Function

C++ is renowned for its efficiency. Learn how Algorithm Adjacent Find Function enables low-level control and high-performance computing in the tutorial below.

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

Example

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);

Example


## 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;

}

Example


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


## 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;

}

Example


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.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience