home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 May / VPR9705A.ISO / VPR_DATA / PROGRAM / CBTRIAL / SETUP / DATA.Z / ADJ_DIFF.CPP < prev    next >
Text File  |  1997-02-14  |  1KB  |  43 lines

  1.  #include <numeric>       // For adjacent_difference.
  2.  #include <vector>        // For vector.
  3.  #include <functional>    // For times.
  4.  
  5.  using namespace std;
  6.  
  7.  int main ()
  8.  {
  9.    //
  10.    // Initialize a vector of ints from an array.
  11.    //
  12.    int arr[10] = {1,1,2,3,5,8,13,21,34,55};
  13.    vector<int> v(arr+0, arr+10);
  14.    //
  15.    // Two uninitialized vectors for storing results.
  16.    //
  17.    vector<int> diffs(10), prods(10);
  18.    //
  19.    // Calculate difference(s) using default operator (minus).
  20.    //
  21.    adjacent_difference(v.begin(),v.end(),diffs.begin());
  22.    //
  23.    // Calculate difference(s) using the times operator.
  24.    //
  25.    adjacent_difference(v.begin(), v.end(), prods.begin(), times<int>());
  26.    //
  27.    // Output the results.
  28.    //
  29.    cout << "For the vector: " << endl << "     ";
  30.    copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
  31.    cout << endl << endl;
  32.  
  33.    cout << "The differences between adjacent elements are: " << endl << "    ";
  34.    copy(diffs.begin(),diffs.end(), ostream_iterator<int>(cout," "));
  35.    cout << endl << endl;
  36.  
  37.    cout << "The products of adjacent elements are: " << endl << "     ";
  38.    copy(prods.begin(),prods.end(), ostream_iterator<int>(cout," "));
  39.    cout << endl;
  40.  
  41.    return 0;
  42.  }
  43.