Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

adjacent_difference


Generalized Numeric Operation

Summary

Outputs a sequence of the differences between each adjacent pair of elements in a range.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

None

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 calculated differences between adjacent elements. For instance, applying adjacent_difference to {1,2,3,5} produces 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:

or

result is assigned the value of *first.

binary_op should not have side effects.

adjacent_difference returns result + (last - first).

result can be equal to first. This allows you to place the results of applying adjacent_difference into the original sequence.

Complexity

This algorithm performs exactly (last-first) - 1 applications of the default operation (-) or binary_op.

Example

Program Output

Warnings

If your compiler does not support default template parameters, then you always need to supply the Allocator template argument. For instance, you have to write:

instead of:

If your compiler does not support namespaces, then you do not need the using declaration for std.



Previous fileTop of documentContentsIndexNext file
©Copyright 1998, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.
OEM Release, June 1998