Previous fileTop of DocumentContentsIndex pageNext file
Apache C++ Standard Library Reference Guide

adjacent_difference()

Library:  Numerics


Function

Local Index

No Entries

Summary

A generalized numeric operation that outputs a sequence of the differences between each adjacent pair of elements in a range

Synopsis

#include <numeric>

namespace std {
  template <class InputIterator, class OutputIterator>
  OutputIterator adjacent_difference(InputIterator start,
                                     InputIterator finish,
                                     OutputIterator result);
  template <class InputIterator,
            class OutputIterator,
            class BinaryOperation>
  OutputIterator adjacent_difference(InputIterator start,
                                     InputIterator finish,
                                     OutputIterator result,
                                     BinaryOperation bin_op);
}

Description

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}.

Complexity

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

Example

Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 26.4.4



Previous fileTop of DocumentContentsIndex pageNext file