CPP STL Tutorial : C++ Min/Max operation that can be performed on containers

min : It will return the smallest
max : It will return the largest
minmax  : It will return smallest and largest elements
min_element : It will return smallest element in range
max_element : It will return largest element in range
minmax_element  : It will return smallest and largest elements in range
To use these  operations, below header should be included:
#include <algorithm>

Example:

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

//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

int main () 
{
  std::vector<int> v = {10,20,25, 30, 5,15};

  std::cout << "min(1,2)==" << std::min(1,2) << '\n';
  std::cout << "max(1,2)==" << std::max(1,2) << '\n';

  std::cout << "The smallest element is " << *std::min_element(v.begin(), v.end()) << '\n';
  std::cout << "The largest element is "  << *std::max_element(v.begin(), v.end()) << '\n';

   auto result = std::minmax({10,20,25, 30, 5,15});
   std::cout << result.first << ' ' << result.second << '\n';
   
  return 0;
}
Output:
min(1,2)==1
max(1,2)==2
The smallest element is 5
The largest element is 30
5 30
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *