# std::minmax() and std::minmax_element() in C++ STL

C++ defined functions to get smallest and largest elements among 2 or in a container using different functions. But there are also functions that are used to get both smallest and largest element using a single function, “minmax()” function achieves this task for us. This function is defined in “algorithm” header file. This article would deal in its implementation and other related functions.

1. minmax(a, b): This function returns a pair, in which 1st element is of minimum of the two elements and the 2nd element is maximum of 2 elements.
2. minmax(array of elements): This function returns similarly as 1st version. Only difference is that in this version, the accepted argument is a list of integers/strings among which maximum and minimum are obtained. Useful in cases when we need to find maximum and minimum elements in list without sorting.
 `// C++ code to demonstrate the working of minmax() ` ` `  `#include ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `     `  `// declaring pair to catch the return value ` `pair<``int``, ``int``> mnmx; ` ` `  `// Using minmax(a, b)    ` `mnmx = minmax(53, 23); ` `     `  `// printing minimum and maximum values ` `cout << ``"The minimum value obtained is : "``; ` `cout << mnmx.first; ` `cout << ``"\nThe maximum value obtained is : "``; ` `cout << mnmx.second ; ` `     `  `// Using minmax((array of elements)  ` `mnmx = minmax({2, 5, 1, 6, 3}); ` `     `  `// printing minimum and maximum values. ` `cout << ``"\n\nThe minimum value obtained is : "``; ` `cout << mnmx.first; ` `cout << ``"\nThe maximum value obtained is : "``; ` `cout << mnmx.second; ` `     `  `} `

Output:

```The minimum value obtained is : 23
The maximum value obtained is : 53

The minimum value obtained is : 1
The maximum value obtained is : 6
```
3. minmax_element(): This purpose of this function is same as above functions i.e to find minimum and maximum element. But it differs in return type and accepted argument. This function accepts start and end pointer as its argument and is used to find maximum and minimum element in a range. This function returns pair pointer, whose 1st element points to the position of minimum element in the range and 2nd element points to the position of maximum element in the range. If there are more than 1 minimum numbers, then the 1st element points to first occurring element. If there are more than 1 maximum numbers, then the 2nd element points to last occurring element.
 `// C++ code to demonstrate the working of minmax_element() ` ` `  `#include ` `#include ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `     `  `    ``// initializing vector of integers ` `    ``vector<``int``> vi = { 5, 3, 4, 4, 3, 5, 3 }; ` `         `  `    ``// declaring pair pointer to catch the return value ` `    ``pair::iterator, vector<``int``>::iterator> mnmx; ` `         `  `    ``// using minmax_element() to find ` `    ``// minimum and maximum element ` `    ``// between 0th and 3rd number ` `    ``mnmx = minmax_element(vi.begin(), vi.begin() + 4); ` `         `  `    ``// printing position of minimum and maximum values. ` `    ``cout << ``"The minimum value position obtained is : "``; ` `    ``cout << mnmx.first - vi.begin() << endl; ` `         `  `    ``cout << ``"The maximum value position obtained is : "``; ` `    ``cout << mnmx.second - vi.begin() << endl; ` `         `  `    ``cout << endl; ` `         `  `    ``// using duplicated ` `    ``// prints 1 and 5 respectively ` `    ``mnmx = minmax_element(vi.begin(), vi.end()); ` `         `  `    ``// printing position of minimum and maximum values. ` `    ``cout << ``"The minimum value position obtained is : "``; ` `    ``cout << mnmx.first - vi.begin() << endl; ` `         `  `    ``cout << ``"The maximum value position obtained is : "``; ` `    ``cout << mnmx.second - vi.begin()<< endl; ` `     `  `} `

Output:

```The minimum value position obtained is : 1
The maximum value position obtained is : 0

The minimum value position obtained is : 1
The maximum value position obtained is : 5
```

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.