Finding the Greatest or Least Value in a Container using max_element and min_element algorithm - C++ STL Algorithm

C++ examples for STL Algorithm:max_element

Description

Finding the Greatest or Least Value in a Container using max_element and min_element algorithm

Demo Code

#include <algorithm>
#include <vector>
#include <iostream>

using namespace std;

int getMaxInt(vector<int>& v) {
  return *max_element(v.begin(), v.end());
}

int getMinInt(vector<int>& v) {
  return *min_element(v.begin(), v.end());
}

int main() {/*w  w w .  ja  va 2 s.  c  o m*/
  vector<int> v;
  for (int i=10; i < 20; ++i) 
     v.push_back(i);
  cout << "min integer = " << getMinInt(v) << endl;
  cout << "max integer = " << getMaxInt(v) << endl;
}

Result


Related Tutorials