Skip to content
Related Articles

Related Articles

Improve Article

std::adjacent_find in C++

  • Difficulty Level : Hard
  • Last Updated : 20 Aug, 2021

Searches the range [first, last) for the first occurrence of two consecutive elements that match, and returns an iterator to the first of these two elements, or last if no such pair is found. Elements are compared using the given binary predicate p or using ==. 
There are two possible implementations of the function as given below: 

1. Without binary predicate: 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
ForwardIt adjacent_find( ForwardIt first, ForwardIt last );
first, last : the range of elements to examine

Example : 
Given a sorted array of n elements containing all unique elements but one, the task is to find the repeating element in the array. 

Examples: 



Input :  arr[] = { 1, 2, 3, 4, 4}
Output :  4

Input :  arr[] = { 1, 1, 2, 3, 4}
Output :  1

We have discussed this problem with other approaches here.

C++




// C++ Program to find the only
// repeating element in sorted array
// using std :: adjacent_find
// without predicate
#include <iostream>
#include <algorithm>
 
int main()
{
    // Sorted Array with a repeated element
    int A[] = { 10, 13, 16, 16, 18 };
 
    // Size of the array
    int n = sizeof(A) / sizeof(A[0]);
 
    // Iterator pointer which points to the address of the repeated element
    int* it = std::adjacent_find(A, A + n);
 
    // Printing the result
    std::cout << *it;
}

Output: 

16

2. With binary predicate: 

ForwardIt adjacent_find( ForwardIt first, ForwardIt last, BinaryPredicate p );
first, last : the range of elements to examine
p :  binary predicate which returns true 
if the elements should be treated as equal. 

Return value :
An iterator to the first of the first pair of identical elements, '
that is, the first iterator it such that *it == *(it+1) for the first 
version or p(*it, *(it + 1)) != false for the second version.
If no such elements are found, last is returned.

Example: 
Given a container of size n, and a range between [0 … n], write a program to check if it is sorted in ascending order or not. Equal values are allowed in array and two consecutive equal values are considered sorted. 

Input : 2 5 9 4      // Range = 3
Output : Sorted in given range.

Input : 3 5 1 9     // Range = 3
Output : Not sorted in given range.

C++




// CPP program to illustrate
// std :: adjacent_find'
// with binary predicate
#include <algorithm>
#include <iostream>
#include <vector>
 
int main()
{
    std::vector<int> vec{ 0, 1, 2, 5, 40, 40, 41, 41, 5 };
 
    // Index 0 to 4
    int range1 = 5;
 
    // Index 0 to 8
    int range2 = 9;
 
    std::vector<int>::iterator it;
 
    // Iterating from 0 to range1,
    // till we get a decreasing element
    it = std::adjacent_find(vec.begin(),
                            vec.begin() + range1, std::greater<int>());
 
    if (it == vec.begin() + range1)
    {
        std::cout << "Sorted in the range : " << range1 << std::endl;
    }
 
    else
    {
        std::cout << "Not sorted in the range : " << range1 << std::endl;
    }
 
    // Iterating from 0 to range2,
    // till we get a decreasing element
    it = std::adjacent_find(vec.begin(),
                            vec.begin() + range2, std::greater<int>());
 
    if (it == vec.begin() + range2)
    {
        std::cout << "Sorted in the range : " << range2 << std::endl;
    }
 
    else
    {
        std::cout << "Not sorted in the range : " << range2 << std::endl;
    }
}

Output: 

Sorted in the range : 5
Not sorted in the range : 9

This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :