We understand that the C++ programming language includes a comprehensive Standard Template Library that offers a wide range of pre-defined functionalities. In order to determine the maximum or minimum value among the elements of an array, whether inputted by the user or predefined by testers/developers, the standard min function or the max function is utilized.
It may become tedious or repetitive to manually write code to determine both values simultaneously. Hence, C++ STL provides a convenient solution with the std minmax and minmax_element functions. These functions can take input in the form of variables holding numerical values, integers, or even arrays of elements.
C++ Code for minmax Function
// Here, we are writing down the C++ programming language to demonstrate
// the concept of std::minmax() and std::minmax_element() in C++ STL
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
// the below code snippet implements declaring the pair
// which is useful in catching the return value thrown after operations
pair<int, int> mnmx;
// the below code snippet helps us with using min and max functions passed
// as two parameters
mnmx = minmax(63, 33);
// the below code snippet helps us with using min and max functions passed
// as two parameters and printing them down on our output screens
cout << "minimum value is : ";
cout << mnmx.first;
cout << "\nmaximum value is : ";
cout << mnmx.second ;
// the below code snippet helps us with using min and max functions passed
// as two parameters here, they are being given as the array of elements
mnmx = minmax({12, 15, 11, 16, 13});
// the below code snippet helps us with using min and max functions passed
// as two parameters and printing them down on our output screens
cout << "\nminimum value: ";
cout << mix.first;
cout << "\nmaximum value : ";
cout << mnmx.second;
}
Output:
/ tmp / 1Lj mBg VyK h.o
The minimum value is : 33
The maximum value is : 63
minimum value : 11
maximum value : 16
C++ Code for minmax_element Function
// Here, we are writing down the C++ programming language to demonstrate
// the concept of std::minmax() and std::minmax_element() in C++ STL
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
// the below code snippet helps us with vectors being passed as the
// data type integer and values ranging from only single integers
vector<int> vi = { 15, 13, 14, 14, 13, 15, 13 };
// the below code snippet helps us with declaring the pointer variable
// of integer data type where one vector being iterator other is a maxx
// these pointer variables return us a value which will then be caught
pair<vector<int>::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() + 14);
// the below code snippet helps us with using min and max functions
//passed
// as two parameters and printing their positions down on our output
//screens
cout << "position of the minimum value obtained after operating is : ";
cout << mnmx.first - vi.begin() << endl;
cout << "position of the maximum value obtained after operating is : ";
cout << mnmx.second - vi.begin() << endl;
cout << endl;
// here, we are using the duplicates
// the below code snippet prints us 11 and 15 respectively as outputs
mnmx = minmax_element(vi.begin(), vi.end());
// the below code snippet helps us with using min and max functions
//passed
// as two parameters and printing them down on our output screens
cout << "position of the minimum value obtained after operating is : ";
cout << mnmx.first - vi.begin() << endl;
cout << "position of the maximum value obtained after operating is : ";
cout << mnmx.second - vi.begin()<< endl;
}
Output:
/ tmp /1 LjmB gVy Kh.o
position of the minimum value obtained after operating is : 7
position of the maximum value received after working is : 10
position of the minimum value obtained after operating is : 1
position of the maximum value received after working is : 5