STL algorithms

10.3 - Min and max


The std::max and std::min functions return the maximum and minimum argument respectively.

There are two versions of the functions:

  • The std::max(a, b) version, which receives two arguments.
  • The std::max({a, b, ..., z}) version, which receives two or more arguments enclosed within an initializer list.

std::max and std::min are more concise than using if-else or a ternary operator, especially for more than two elements.

Task

Write a program which receives three integers and prints out the maximum and minimum in a line each.

  • Get the integers using std::cin >> a >> b >> c.
  • Get the maximum and minimum elements using std::max({a, b, c}) and std::min({a, b, c}) and print them out.
#include <iostream> #include <vector> #include <algorithm> int main() { }