We are already aware that the C++ programming language has a Standard Template Library which is vast like an ocean and has many pre-defined functionalities. To get the answer of which among the array elements passed either as an input by the user or pre-defined by the testers/developers is maximum or minimum amongst them, we use the standard minimum function or the entire function.
It might get a little overwriting or using the code, again and again, to know both values at a time. So C++ STL has a solution for us in the form of std minmax and minmax_element functions. Both functions accept parameters in the form either variables, which are of any natural numbers, integers and also an array 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