Source of transform2a.cpp


  1: //transform2a.cpp

  3: #include <iostream>
  4: #include <vector>
  5: #include <deque>
  6: #include <list>
  7: #include <algorithm>
  8: using namespace std;


 11: /**
 12: Finds the product of two integers.
 13: Pre:\n a and b contain integer values.
 14: Post:\n The product of a and b has been returned.
 15: */
 16: int product
 17: (
 18:     int a, //in
 19:     int b  //in
 20: )
 21: {
 22:     return a*b;
 23: }


 26: int main()
 27: {
 28:     cout << "\nThis program illustrates the use of the STL transform "
 29:         "algorithm (extended\nversion) to multiply the values in a "
 30:         "range of integers from a vector with\ncorresponding values "
 31:         "in a range of integers from a deque and write the\nproducts "
 32:         "to a range within a list of intgers.";
 33:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 35:     int a1[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
 36:     vector<int> v(a1, a1+10);
 37:     cout << "\nHere are the values in the vector:\n";
 38:     for (vector<int>::size_type i=0; i<v.size(); i++)
 39:         cout << v.at(i) << " ";;
 40:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 42:     int a2[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
 43:     deque<int> d(a2, a2+10);
 44:     cout << "\nHere are the values in the deque:\n";
 45:     for (deque<int>::size_type i=0; i<d.size(); i++)
 46:         cout << d.at(i) << " ";;
 47:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 49:     int a3[] = {-1, -2 , -3, -4, -5, -6, -7, -8, -9, -10};
 50:     list<int> lst(a3, a3+10);
 51:     cout << "\nHere are the values in the list:\n";
 52:     list<int>::iterator p = lst.begin();
 53:     while (p != lst.end()) cout << *p++ << " ";
 54:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 56:     cout << "\nNow we multiply five values from the vector, starting "
 57:         "with the fourth value,\nwith five values from the deque, "
 58:         "starting with the second value, and put the\nresults in the "
 59:         "list, starting at the third position.";
 60:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 62:     p = lst.begin();
 63:     p++;
 64:     p++;

 66:     cout << "\nThe iterator returned by the algorithm is pointing "
 67:         "at the value "
 68:         << *transform(v.begin()+3, v.begin()+8, d.begin()+1, p, product)
 69:         << " in the list,\nand here are all of the values in the list "
 70:         "now:\n";
 71:     p = lst.begin();
 72:     while (p != lst.end()) cout << *p++ << " ";
 73:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 74: }