## adjacent_difference
## Prototype
template <class InputIterator, class OutputIterator> OutputIterator adjacent_difference(InputIterator first, InputIterator last, OutputIterator result); template <class InputIterator, class OutputIterator, class BinaryFunction> OutputIterator adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryFunction binary_op); ## Description
The first version of ## DefinitionDefined in the standard header numeric, and in the nonstandard backward-compatibility header algo.h. ## Requirements on typesFor the first version: -
`ForwardIterator` is a model of ForwardIterator. -
`OutputIterator` is a model of OutputIterator. -
If
`x` and`y` are objects of`ForwardIterator` 's value type, then`x - y` is defined. -
`InputIterator` s value type is convertible to a type in`OutputIterator` 's set of value types. -
The return type of
`x - y` is convertible to a type in`OutputIterator` 's set of value types.
For the second version: -
`ForwardIterator` is a model of ForwardIterator. -
`OutputIterator` is a model of OutputIterator. -
`BinaryFunction` is a model of BinaryFunction. -
`InputIterator` 's value type is convertible to a`BinaryFunction` 's first argument type and second argument type. -
`InputIterator` s value type is convertible to a type in`OutputIterator` 's set of value types. -
`BinaryFunction` 's result type is convertible to a type in`OutputIterator` 's set of value types.
## Preconditions-
`[first, last)` is a valid range. -
`[result, result + (last - first))` is a valid range.
## ComplexityLinear. Zero applications of the binary operation if ## Exampleint main() { int A[] = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100}; const int N = sizeof(A) / sizeof(int); int B[N]; cout << "A[]: "; copy(A, A + N, ostream_iterator<int>(cout, " ")); cout << endl; adjacent_difference(A, A + N, B); cout << "Differences: "; copy(B, B + N, ostream_iterator<int>(cout, " ")); cout << endl; cout << "Reconstruct: "; partial_sum(B, B + N, ostream_iterator<int>(cout, " ")); cout << endl; } ## Notes[1] The reason it is useful to store the value of the first element, as well as simply storing the differences, is that this provides enough information to reconstruct the input range. In particular, if addition and subtraction have the usual arithmetic definitions, then [2] Note that ## See also |