C++ std::adjacent_find() Function

Definition
In C++, std::adjacent_find() scans a range for the first pair of neighbours that match. It returns an iterator to the first element of that pair.
By default it checks for equal neighbours. You can pass your own rule to find other patterns, such as a sudden jump in value.
Syntax
Add #include <algorithm>, then call std::adjacent_find(). Start with these two lines.
std::adjacent_find(v.begin(), v.end());
std::adjacent_find(v.begin(), v.end(), pred);
| Call | What it does |
|---|
std::adjacent_find(v.begin(), v.end()); | Find the first two equal neighbours. |
std::adjacent_find(v.begin(), v.end(), pred); | Find the first pair where pred(a, b) is true. |
Library declarations
template<class ForwardIt>
ForwardIt adjacent_find(ForwardIt first, ForwardIt last);
template<class ForwardIt, class BinaryPred>
ForwardIt adjacent_find(ForwardIt first, ForwardIt last, BinaryPred p);
Parameters
| Parameter | Description |
|---|
| first, last | Required. The range to scan. |
| p | Optional. A function that takes two neighbours and returns true when they form the pair you want. |
Return Value
| Return Value | Description |
|---|
| Iterator | Points at the first element of the first matching pair. |
last | Returned when no pair matches, or the range has fewer than two elements. |
C++ Examples
Example 1 — Duplicate neighbours
Find the first repeated value that sits next to itself.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 2, 3};
auto it = std::adjacent_find(v.begin(), v.end());
if (it != v.end()) {
std::cout << "First duplicate pair at index " << (it - v.begin())
<< " (value " << *it << ")\n";
}
return 0;
}
First duplicate pair at index 1 (value 2)
How It Works
- The function compares 1 with 2, then 2 with 2.
- The second pair is equal, so it stops and returns the iterator to the first 2.
- Subtracting
v.begin() gives index 1.
Example 2 — No equal neighbours
Handle a range with no equal pair.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4};
auto it = std::adjacent_find(v.begin(), v.end());
if (it == v.end()) {
std::cout << "No equal neighbours\n";
}
return 0;
}
How It Works
- All values are different, so no pair of neighbours is equal.
- Three comparisons are made: 1-2, 2-3 and 3-4. None match.
- The result is
v.end(), so we know the search failed.
Example 3 — Find a big jump
Use your own rule to find where a value more than doubles.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, 5, 6, 15, 16};
auto it = std::adjacent_find(v.begin(), v.end(),
[](int a, int b) { return b > a * 2; });
if (it != v.end()) {
std::cout << "Big jump after index " << (it - v.begin()) << ": "
<< *it << " -> " << *(it + 1) << '\n';
}
return 0;
}
Big jump after index 2: 6 -> 15
How It Works
- The lambda receives two neighbours. It is true when the second is more than twice the first.
- 3 to 5 and 5 to 6 are small steps, but 6 to 15 is a jump.
*it is 6 and *(it + 1) is 15, the two values of the pair.
Frequently Asked Questions
In C++, std::adjacent_find() finds the first two neighbouring elements that are equal. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns an iterator to the first element of the first matching pair, or the last iterator if there is none.
No. It only checks neighbours and stops at the first pair. Sort the data first if you want duplicates to sit together.
It returns the last iterator, because there is no pair to find.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful