C++ std::adjacent_difference() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
C++98

Definition

In C++, std::adjacent_difference() compares each element with the one right before it. For every position i it writes a[i] - a[i-1]. The very first element has no left neighbour, so it is copied unchanged.

For the input 2 5 9 14 the output is 2 3 4 5. It is the opposite of std::partial_sum(): running totals turn back into the original numbers. You can pass your own operation to measure gaps in other ways. Include <numeric> to use it.

This function is declared in the <numeric> header. It exists since C++98, so every compiler supports it. It is a free function, called as std::adjacent_difference(...), not with a dot.

Syntax

Give the input range and where to write. A custom operation is optional.

C++
auto e1 = std::adjacent_difference(first, last, d_first);
auto e2 = std::adjacent_difference(first, last, d_first, op);
CallWhat it does
adjacent_difference(first, last, d_first)Writes a0, then a1 - a0, then a2 - a1, and so on.
adjacent_difference(first, last, d_first, op)Writes a0, then op(a1, a0), then op(a2, a1), and so on.

The output may start at the same place as the input, so you can change a vector in place. The output has the same number of elements as the input.

Library declarations
C++
template<class InputIt, class OutputIt>
OutputIt adjacent_difference(InputIt first, InputIt last, OutputIt d_first);

template<class InputIt, class OutputIt, class BinaryOp>
OutputIt adjacent_difference(InputIt first, InputIt last,
                             OutputIt d_first, BinaryOp op);

// C++17 adds versions with a leading ExecutionPolicy&& argument.

Parameters

ParameterDescription
first, lastRequired. The input range [first, last).
d_firstRequired. The start of the output. It needs room for the same number of elements as the input.
opOptional. A binary function called as op(current, previous). The default is subtraction.

Return Value

Return ValueDescription
Output iteratorAn iterator one past the last element written. For an empty input, d_first is returned.

C++ Examples

Example 1 — Differences Between Neighbours

Write how much each number grew compared with the one before it.

C++
#include <iostream>
#include <numeric>
#include <vector>

int main() {
    std::vector<int> v = {2, 5, 9, 14};
    std::vector<int> out(v.size());

    std::adjacent_difference(v.begin(), v.end(), out.begin());

    for (int n : out) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The first element has no left neighbour, so 2 is copied to the output as it is.
  2. The second output is 5-2 = 3, the third is 9-5 = 4, and the fourth is 14-9 = 5.
  3. The output has four elements, the same as the input. Each value after the first tells you the size of one step.

Example 2 — Undo partial_sum()

Running totals can be turned back into the original numbers with adjacent_difference().

C++
#include <iostream>
#include <numeric>
#include <vector>

int main() {
    std::vector<int> totals = {1, 3, 6, 10, 15};
    std::vector<int> original(totals.size());

    std::adjacent_difference(totals.begin(), totals.end(),
                             original.begin());

    for (int n : original) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The vector totals holds the running sums of 1 2 3 4 5. This is what partial_sum() would have written.
  2. The first value 1 is copied. Then 3-1 = 2, 6-3 = 3, 10-6 = 4, and 15-10 = 5.
  3. This works because each running total grew by exactly one original number. The two functions undo each other.

Example 3 — Custom Operation: Add Neighbours

Give your own operation to add each element to its left neighbour instead of subtracting.

C++
#include <functional>
#include <iostream>
#include <numeric>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 3, 4};
    std::vector<int> out(v.size());

    std::adjacent_difference(v.begin(), v.end(), out.begin(),
                             std::plus<int>());

    for (int n : out) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. std::plus<int>() replaces the default subtraction, so each output is current + previous.
  2. The first value is still copied as it is: 1. The operation is never called for the first element.
  3. The next outputs are 2+1 = 3, 3+2 = 5, and 4+3 = 7. Despite its name, the function can do any pair-wise calculation.

Frequently Asked Questions

It writes the first element unchanged and then, for each following position, the difference between that element and the one before it. For 2 5 9 14 the output is 2 3 4 5.
They are inverses. If you run partial_sum() on a range and then adjacent_difference() on the result, you get the original range back.
The first element has no left neighbour to subtract. The function copies it as it is, which also makes the inverse with partial_sum() work.
Yes. You can pass the input start as the output start. The function remembers the previous value before it overwrites it, so the result is still correct.
Yes. Pass a binary function as the last argument. It is called as op(current, previous), so a lambda like [](int a, int b) { return a * b; } would multiply neighbours.

Next: exclusive_scan()

Write running totals that leave out the current element, starting from init (C++17) with <numeric>.

exclusive_scan() 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.

8 people found this page helpful