The C++ Standard Library consists of the header, <algorithm>
which defines a collection of functions (principally designed to deal with a range of elements). minmax()
is a method in the STL that returns the smallest and largest element among two elements or, an initializer list
Syntax:
minmax( data_type a, data_type b, comparator); minmax( init_list, comparator ); // comparator is optional
Parameters: The minmax()
method accepts 4 parameters
- a, b: Values to compare
- init_list: Initializer list with the values to compare
- comparator: Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool.
Return value: A pair object, whose first member (pair::first) is the smallest element, and the second member (pair::second) is the largest
Example of minmax() method
#include<iostream> #include<algorithm> using namespace std; int main() { pair<int, int> p; p = minmax(33, 10); cout << "The minimum is : "<<p.first<<endl; cout << "The maximum is : "<<p.second<<endl; p = minmax({5,6,1,3,4}); cout << "The minimum is : "<<p.first<<endl; cout << "The maximum is : "<<p.second<<endl; }
Output:
The minimum is : 10 The maximum is : 33 The minimum is : 1 The maximum is : 6