Apache C++ Standard Library Reference Guide

## adjacent_difference()

Library: Numerics

    Function

- Local Index
- Summary
- Synopsis
- Description
- Complexity
- Example
- Standards Conformance

### 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

    //
    //  adj_diff.cpp
    //

    #include <numeric>       // for adjacent_difference
    #include <vector>        // for vector
    #include <iostream>      // for cout
    #include <functional>    // for multiplies

    int main ()
    {
        // Typedefs for convenience.
        typedef std::vector<long, std::allocator<long> > Vector;

        typedef std::ostream_iterator <Vector::value_type,
                                       char,
                                       std::char_traits<char> >
                os_iter;

        // Initialize a Vector of ints from an array.
        const Vector::value_type arr [] = {
            1, 1, 2, 3, 5, 8, 13, 21, 34, 55
        };

        Vector v (arr + 0, arr + sizeof arr / sizeof *arr);

        // Two vectors initialized to all zeros
        // for storing results.
        Vector diffs (v.size ()),
               prods (v.size ());

        // Calculate difference(s) using
        // default operator (minus).
        std::adjacent_difference (v.begin (), v.end (),
                                  diffs.begin ());

        // Calculate difference (s) using the times operator.
        std::adjacent_difference (v.begin (), v.end (),
                         prods.begin (),
                         std::multiplies<Vector::value_type> ());

        // Create an ostream_iterator object (for convenience).
        os_iter osit (std::cout, " ");

        // Output the results.
        std::cout << "For the vector: " << std::endl << "     ";
        std::copy (v.begin (), v.end (), osit);

        std::cout << std::endl << std::endl;

        std::cout << "The differences between adjacent elements"
                  << " are: " << std::endl << "    ";

        std::copy (diffs.begin (), diffs.end (), osit);
        std::cout << std::endl << std::endl;

        std::cout << "The products of adjacent elements are: "
                  << std::endl << "     ";

        std::copy (prods.begin (), prods.end (), osit);
        std::cout << std::endl;

        return 0;
    }
    Program Output:
    For the vector:
         1 1 2 3 5 8 13 21 34 55

    The differences between adjacent elements are:
        1 0 1 1 2 3 5 8 13 21

    The products of adjacent elements are:
         1 1 2 6 15 40 104 273 714 1870

### Standards Conformance

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