Source of adjacent_difference1a.cpp


  1: //adjacent_difference1a.cpp

  3: #include <iostream>
  4: #include <vector>
  5: #include <numeric>
  6: using namespace std;

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the use of the STL "
 11:         "adjacent_difference() algorithm\n(default version) from "
 12:         "<numeric> to find the differences between successive\npairs "
 13:         "of integers in one vector, and write them to a second vector.";
 14:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 16:     vector<int> v1{10, 12, 16, 22, 30}; //since C++11
 17:     cout << "\nHere are the values in the first vector:\n";
 18:     for (int i : v1) cout << i << " "; //since C++11
 19:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 21:     vector<int> v2{1, 3, 5, 7, 9, 11, 13};
 22:     cout << "\nAnd here are the values in the second vector:\n";
 23:     for (int i : v2) cout << i << " "; //since C++11
 24:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 26:     cout << "\nNow we use ajacent_difference() to compute the "
 27:         "adjacent differences for the\nentire first vector, and "
 28:         "write them to the second vector, starting at the\nsecond "
 29:         "position of the second vector.";
 30:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 32:         //since C++11 ...
 33:     auto p = adjacent_difference(v1.begin(), v1.end(), v2.begin()+1);

 35:     cout << "\nHere are the integer values in the second vector "
 36:         "after this has been done:\n";
 37:     for (int i : v2) cout << i << " "; //since C++11
 38:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 40:     cout << "\nThe value pointed to by the iterator "
 41:         "returned by adjacent_difference() is ";
 42:     cout << *p << ".";
 43:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 44: }