Source of max_element1a.cpp


  1: //max_element1a.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_element() "
 11:             "algorithm (default\nversion) to find the maximum value in "
 12:             "a vector of integers.";
 13:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

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

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

 23:     cout << "\nThe maximum value in the vector is "
 24:         << *max_element(v.begin(), v.end()) << ".";
 25:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 26: }