Source of inner_product1a.cpp


  1: //inner_product1a.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:         "inner_product() algorithm (default\nversion) to compute the "
 12:         "inner product of two ranges of integer values from two "
 13:         "\nvectors of integers.";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 16:     int a1[] = {2, 1, 4, 3, 6, 5};
 17:     vector<int> v1(a1, a1+6);

 19:     cout << "\nHere are the contents of v1:\n";
 20:     for (vector<int>::size_type i=0; i<v1.size(); i++)
 21:         cout << v1.at(i) << " ";
 22:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 24:     int a2[] = {1, 2, 3, 4, 5, 6, 7, 8};
 25:     vector<int> v2(a2, a2+8);

 27:     cout << "\nHere are the contents of v2:\n";
 28:     for (vector<int>::size_type i=0; i<v2.size(); i++)
 29:         cout << v2.at(i) << " ";
 30:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 32:     cout << "\nThe inner product of the values in v1 with the values "
 33:         "\nstarting at the beginning of v2 is "
 34:         << inner_product(v1.begin(), v1.end(), v2.begin(), 0) << ".";
 35:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 37:     cout << "\nThe inner product of the the last three values of each "
 38:         "vector,\ncombined with an initial value of 10, is "
 39:         << inner_product(v1.end()-3, v1.end(), v2.end()-3, 10) << ".";
 40:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 41: }