Source of max1a.cpp


  1: //max1a.cpp

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

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the use of the STL max() "
 11:             "algorithm (default version)\nto find the maximum of two "
 12:             "integer literal values, the maximum of two integer\nvalues "
 13:             "stored in simple integer variables, and (in two different "
 14:             "ways) the\nmaximum of two integer values stored in a vector "
 15:             "of integers.";
 16:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 18:     cout << "\nFirst, we find the maximum of two integer "
 19:         "literal values.\n";
 20:     cout << "max(12, -3) = " << max(12, -3);
 21:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 23:     int first = 7;
 24:     int second = 10;
 25:     cout << "\nNext, we find the maximum of two integer "
 26:         "\nvalues stored in simple integer variables.";
 27:     cout << "\nfirst = " << first << "    second = " << second << endl;
 28:     cout << "max(first, second) = " << max(first, second);
 29:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 31:     int a[] = {2, 6, 10, 8, 4};
 32:     vector<int> v(a, a+5);

 34:     cout << "\nHere are the integer values in the vector:\n";
 35:     for (vector<int>::size_type i=0; i<v.size(); i++)
 36:         cout << v.at(i) << " ";
 37:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 39:     cout << "\nNow we find the maximum of the first and last values "
 40:         "\nstored in the vector of integers using one approach.";
 41:     cout << "\nmax(v.at(0), v.at(4)) = " << max(v.at(0), v.at(4));
 42:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 44:     cout << "\nFinally, we find the maximum of the first and last values "
 45:         "\nstored in the vector of integers using a second approach.";
 46:     cout << "\nmax(*v.begin(), *(v.end()-1)) = "
 47:         << max(*v.begin(), *(v.end()-1));
 48:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 49: }