std::adjacent_find in C++
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:
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
Similar Reads
std::adjacent_difference in C++
Compute adjacent difference of range Assigns to every element in the range starting at result, the difference between its corresponding element in the range [first, last] and the one preceding it (except for *result, which is assigned *first). If x represents an element in [first, last] and y repres
5 min read
std::find_end in C++
std::find_end is used to find the last occurrence of a sub-sequence inside a container. It searches the range [first1,last1) for the last occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found. It is similar to std::se
6 min read
std::advance in C++
std::advance advances the iterator 'it' by n element positions. Syntax : template void advance (InputIterator& it, Distance n); it : Iterator to be advanced n : Number of element positions to advance. This shall only be negative for random-access and bidirectional iterators. Return type : None.
2 min read
find() in C++ STL
C++ find() is a built-in function used to find the first occurrence of an element in the given range. It works with any container that supports iterators, such as arrays, vectors, lists, and more. In this article, we will learn about find() function in C++.C++#include <bits/stdc++.h> using nam
2 min read
std::min_element in C++
The std::min_element() in C++ is an STL algorithm that is used to find the minimum element in a given range. This range can be array, vector, list or any other container. It is defined inside the <algorithm> header file. In this article, we will learn about the std::min_element() in C++.Exampl
4 min read
std::next vs std::advance in C++
std::advance and std::next are used to advance the iterator by a certain position, such that we can make the iterator point to a desired position. Although both have same purpose, but their implementation is different from each other. This makes it important for us to understand the difference betwe
3 min read
String find() in C++
In C++, string find() is a built-in library function used to find the first occurrence of a substring in the given string. Letâs take a look at a simple example that shows the how to use this function:C++#include <bits/stdc++.h> using namespace std; int main() { string s = "Welcome to GfG!"; s
4 min read
std::find_first_of in C++
std::find_first_of is used to compare elements between two containers. It compares all the elements in a range [first1,last1) with the elements in the range [first2,last2), and if any of the elements present in the second range is found in the first one , then it returns an iterator to that element.
6 min read
std::bsearch in C++
std::bsearch searches for an element in a sorted array. It performs binary search on the sorted array to search for an element. The std::search function is defined inside the <cstdlib> header file. Syntaxvoid* bsearch(const void* key, const void* ptr, size_t num, size_t size, int (*comp)(const
4 min read
std::next in C++
std::next returns an iterator pointing to the element after being advanced by certain no. of positions. It is defined inside the header file . It does not modify its arguments and returns a copy of the argument advanced by the specified amount. If it is a random-access iterator, the function uses ju
4 min read