If x represents an element in [first,last) and y represents an element in result, the ys can be calculated as:
y0 = x0
y1 = x1 - x0
y2 = x2 - x1
y3 = x3 - x2
y4 = x4 - x3
... ... ...
The default operation is to calculate the difference, but some other operation can be specified as binary_op instead.
The behavior of this function template is equivalent to:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Parameters
- first, last
- Input iterators to the initial and final positions in a sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
- result
- Output iterator to the initial position in the destination sequence where the differences are stored. The range starts at result and shall have a size large enough to contain as many elements as the range above ([first,last)).
- binary_op
- Binary operation taking as arguments two elements of the type pointed by the InputIterator, and returning the result of the replacement for the difference operation. This can either be a pointer to a function or an object whose class overloads operator().
Return value
An iterator pointing to past the last element of the destination sequence where resulting elements have been stored.Example
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
Output:
using default adjacent_difference: 1 1 1 2 4 2 1 using functional operation multiplies: 1 2 6 15 45 99 132 using custom function: 1 3 5 8 14 20 23 |
See also
| accumulate | Accumulate values in range (function template) |
| inner_product | Compute cumulative inner product of range (function template) |
| partial_sum | Compute partial sums of range (function template) |
