adjacent_difference( ) returns a new sequence in which each element is the difference between adjacent elements in the original sequence. : adjacent_difference « STL Algorithms Helper « C++






adjacent_difference( ) returns a new sequence in which each element is the difference between adjacent elements in the original sequence.

  
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
   
int main()
{
  vector<int> v(10), r(10);
  int i;
   
  for(i=0; i<10; i++) {
    v[i] = i*2;
    cout << v[i] << endl;
  }  
   
  adjacent_difference(v.begin(), v.end(), r.begin());
   
  for(i=0; i<10; i++)
    cout << r[i] << endl;
   
  return 0;
}
  
    
  








Related examples in the same category

1.adjacent_difference: print all differences between elements
2.adjacent_difference: print all sums with the predecessors
3.adjacent_difference: print all products between elements
4.Use adjacent_difference to convert elements in a container into relative values