Skip to content. Skip to navigation

ICTP Portal

Sections
You are here: Home Manuals on-line PGI Compiler pgC_lib adjacent_difference
Personal tools
Document Actions

adjacent_difference



Click on the banner to return to the class reference home page.

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} will produce 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:

*(first  + (i - result)) - *(first + (i - result) - 1)

or

binary_op (*(first + (i - result)), *(first + (i - result) - 1))

result is assigned the value of *first.

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

//
// adj_diff.cpp
//
 #include<numeric>       //For adjacent_difference
 #include<vector>        //For vector
 #include<functional>    //For times
 #include <iostream.h>
 int main()
 {
   //
   //Initialize a vector of ints from an array
   //
   int arr[10] = {1,1,2,3,5,8,13,21,34,55};
   vector<int> v(arr,arr+10);
   //
   //Two uninitialized vectors for storing results
   //
   vector<int> diffs(10), prods(10);
   //
   //Calculate difference(s) using default operator (minus)
   //
   adjacent_difference(v.begin(),v.end(),diffs.begin());
   //
   //Calculate difference(s) using the times operator
   //
   adjacent_difference(v.begin(), v.end(), prods.begin(), 
         times<int>());
   //
   //Output the results
   //
   cout << "For the vector: " << endl << "     ";
   copy(v.begin(),v.end(),
        ostream_iterator<int,char>(cout," "));
   cout << endl << endl;
   cout << "The differences between adjacent elements are: " 
        << endl << "     ";
   copy(diffs.begin(),diffs.end(),
        ostream_iterator<int,char>(cout," "));
   cout << endl << endl;
   cout << "The products of adjacent elements are: "
        << endl << "     ";
   copy(prods.begin(),prods.end(),
        ostream_iterator<int,char>(cout," "));
   cout << endl;
   return 0;

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

Warning

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

vector<int,allocator<int> >

instead of:

vector<int>

©Copyright 1996, Rogue Wave Software, Inc.


Powered by Plone This site conforms to the following standards: