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:     int a1[] = {10, 12, 16, 22, 30};
 17:     vector<int> v1(a1, a1+5);
 18:     cout << "\nHere are the values in the first vector:\n";
 19:     for (vector<int>::size_type i=0; i<v1.size(); i++)
 20:         cout << v1.at(i) << " ";
 21:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 23:     int a2[] = {1, 3, 5, 7, 9, 11, 13};
 24:     vector<int> v2(a2, a2+7);
 25:     cout << "\nAnd here are the values in the second vector:\n";
 26:     for (vector<int>::size_type i=0; i<v2.size(); i++)
 27:         cout << v2.at(i) << " ";
 28:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

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

 36:     vector<int>::iterator p;
 37:     p = adjacent_difference(v1.begin(), v1.end(), v2.begin()+1);

 39:     cout << "\nHere are the integer values in the second vector "
 40:         "after this has been done:\n";
 41:     for (vector<int>::size_type i=0; i<v2.size(); i++)
 42:         cout << v2.at(i) << " ";
 43:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 45:     cout << "\nThe value pointed to by the iterator "
 46:         "returned by adjacent_difference() is ";
 47:     cout << *p << ".";
 48:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 49: }