C++ std::adjacent_find() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

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.

C++
std::adjacent_find(v.begin(), v.end());
std::adjacent_find(v.begin(), v.end(), pred);
CallWhat 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
C++
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

ParameterDescription
first, lastRequired. The range to scan.
pOptional. A function that takes two neighbours and returns true when they form the pair you want.

Return Value

Return ValueDescription
IteratorPoints at the first element of the first matching pair.
lastReturned 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.

C++
#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;
}

How It Works

  1. The function compares 1 with 2, then 2 with 2.
  2. The second pair is equal, so it stops and returns the iterator to the first 2.
  3. Subtracting v.begin() gives index 1.

Example 2 — No equal neighbours

Handle a range with no equal pair.

C++
#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

  1. All values are different, so no pair of neighbours is equal.
  2. Three comparisons are made: 1-2, 2-3 and 3-4. None match.
  3. 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.

C++
#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;
}

How It Works

  1. The lambda receives two neighbours. It is true when the second is more than twice the first.
  2. 3 to 5 and 5 to 6 are small steps, but 6 to 15 is a jump.
  3. *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.

Next: all_of()

Checks whether every element in a range matches a condition.

all_of() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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