Source of transform1a.cpp


  1: //transform1a.cpp

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

 10: /**
 11: Finds the cube of its input.
 12: Pre:\n n contains an integer value.
 13: Post:\n The cube of n has been returned.
 14: */
 15: int cube
 16: (
 17:     int n //in
 18: )
 19: {
 20:     return n * n * n;
 21: }


 24: int main()
 25: {
 26:     cout << "\nThis program illustrates the use of the STL transform "
 27:         "algorithm (default\nversion) to find the square root of each "
 28:         "value in a range of integers,\nas well as the cube of each "
 29:         "value in a range of integers."
 30:         
 31:         "\n\nThe square roots are found with a built-in library function "
 32:         "and placed in\na different container from the original values. "
 33:         "The cubes are found with a\nprogrammer-defined function and they "
 34:         "overwrite the original values.";
 35:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 37:     int a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 38:     vector<int> v(a1, a1+10);
 39:     cout << "\nHere are the values in a vector:\n";
 40:     for (vector<int>::size_type i=0; i<v.size(); i++)
 41:         cout << v.at(i) << " ";;
 42:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 44:     double a2[] = {-1.0, -2.0, -3.0, -4.0, -5.0,
 45:                    -6.0, -7.0, -8.0, -9.0, -10.0};
 46:     deque<double> d(a2, a2+10);
 47:     cout << "\nHere are the values in a deque:\n";
 48:     for (deque<double>::size_type i=0; i<d.size(); i++)
 49:         cout << d.at(i) << " ";;
 50:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 52:     deque<double>::iterator p_deque;
 53:     p_deque = transform(v.begin()+1, v.end()-1, d.begin()+1, sqrt);
 54:     cout << "\nHere are the new values in the deque after all but the "
 55:         "values at the ends\nhave been overwritten by the square roots "
 56:         "of the corresponding values from\nthe vector:\n";
 57:     for (deque<double>::size_type i=0; i<d.size(); i++)
 58:         cout << d.at(i) << " ";;
 59:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 61:     cout << "\nAnd the iterator returned by the algorithm is pointing "
 62:         "at the deque value " << *p_deque << ".";
 63:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 65:     vector<int>::iterator p_vector = v.end();
 66:     p_vector = transform(v.begin(), v.end(), v.begin(), cube);
 67:     cout << "\nHere are the cubes of the original values, read now "
 68:         "from the original vector:\n";
 69:     for (vector<int>::size_type i=0; i<v.size(); i++)
 70:         cout << v.at(i) << " ";;
 71:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 73:     if (p_vector == v.end())
 74:         cout << "\nThis time the iterator returned by the algorithm "
 75:         "is pointing to the end of\nthe original vector.";
 76:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 77: }