Standard C++ Library Copyright 1998, Rogue Wave Software, Inc.

adjacent_difference- Outputs a sequence of the differences between each adja- cent pair of elements in a range.

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

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 sup- plying 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 iteratoriin the range [result+1,result+ (last-first)) a value equal to the appropriate one of the following: *(first+ (i-result)) - *(first+ (i-result) -1) orbinary_op(*(first+ (i-result)), *(first+ (i-result) -1))resultis assigned the value of *first.binary_opshould not have side effects.adjacent_differencereturnsresult+ (last-first).resultcan be equal tofirst. This allows you to place the results of applying adjacent_difference into the original sequence.

This algorithm performs exactly (last-first) -1applica- tions of the default operation (-) orbinary_op.

// // adj_diff.cpp // #include<numeric> //For adjacent_difference #include<vector> //For vector #include<functional> //For times #include <iostream> using namespace std; 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; 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

If your compiler does not support default template parame- ters, then you always need to supply theAllocatortemplate argument. For instance, you have to write: vector<int,allocator<int> > instead of: vector<int> If your compiler does not support namespaces, then you do not need the using declaration forstd.