Name

adjacent_difference function template — Computes differences of adjacent elements in a range

Synopsis

template <typename InIter, typename OutIter>
OutIter adjacent_difference(InIter first, InIter last, OutIter result);
template <typename InIter, typename OutIter, typename BinOp>
OutIter adjacent_difference(InIter first, InIter last, OutIter result, 
                              BinOp binary_op);

The adjacent_difference function computes the differences of adjacent elements in the range [first, last) and assigns those differences to the output range starting at result. The second version calls binary_op instead of using the subtraction (-) operator.

Technical Notes

For each i in [first + 1, last) and j in [result, result + (last - first)), assign *j = *i - tmp, in which tmp is initially *first; it becomes *i after each assignment to *j.

The return value is the result iterator pointing to one past the last element written.

The binary_op function or functor must not have any side effects. The result iterator can be the same as first.

Complexity is linear: binary_op is called exactly last - first - 1 times.

Get C++ In a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.