Generic function for returning the minimum and maximum value using max_element and min_element algorithm - C++ STL Algorithm

C++ examples for STL Algorithm:max_element

Description

Generic function for returning the minimum and maximum value using max_element and min_element algorithm

Demo Code

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

using namespace std;

template<class Iter_T, class Value_T>
void computeMinAndMax(Iter_T first, Iter_T last, Value_T& min, Value_T& max) {
  min = *min_element(first, last);/*  ww  w .  j  ava2s  .c  o  m*/
  max = *max_element(first, last);
}

int main() {
  vector<int> v;
  
  for (int i=10; i < 20; ++i) 
     v.push_back(i);
  
  int min = -1;
  int max = -1;
  
  computeMinAndMax(v.begin(), v.end(), min, max);
  
  cout << "min integer = " << min << endl;
  cout << "max integer = " << max << endl;
}

Result


Related Tutorials