Standard C++ Library Class Reference
Click on the banner to return to the Class Reference home page.
©Copyright 1996 Rogue Wave Software
adjacent_difference
Generalized Numeric Operation
Summary
Outputs a sequence of the differences between each adjacent pair of elements in a range.
Contents
Synopsis●
Description●
Complexity●
Example●
Warning●
Synopsis
#include <numeric>
template <class InputIterator, class OutputIterator>
OutputIterator adjacent_difference (InputIterator first,
InputIterator last,
OutputIterator result);
template <class InputIterator,
class OutputIterator,
class BinaryOperation>
OutputIterator adjacent_difference (InputIterator first,
InputIterator last,
OutputIterator result,
BinaryOperation bin_op);
Description
Informally, adjacent_difference fills a sequence with the differences between successive elements in a container. The result is
a sequence in which the first element is equal to the first element of the sequence being processed, and the remaining elements
are equal to the the calculated differences between adjacent elements. For instance, applying adjacent_difference to {1,2,3,5}
will produce a result of {1,1,1,2}.
By default, subtraction is used to compute the difference, but you can supply any binary operator. The binary operator is then
applied to adjacent elements. For example, by supplying the plus (+) operator, the result of applying adjacent_difference to
{1,2,3,5} is the sequence {1,3,5,8}.
Formally, adjacent_difference assigns to every element referred to by iterator i in the range [result + 1, result + (last - first)) a
value equal to the appropriate one of the following:
*(first + (i - result)) - *(first + (i - result) - 1)
or