std::minmax() and std::minmax_element() in C++ STL Last Updated : 21 Mar, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. 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.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. CPP // C++ code to demonstrate the working of minmax() #include<iostream> #include<algorithm> 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 Time Complexity: O(n) where n is the number of elements from which we have to find the minimum and maximum element. Auxiliary Space: O(1) 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. CPP // C++ code to demonstrate the working of minmax_element() #include<iostream> #include<algorithm> #include<vector> 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<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() + 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 Time Complexity: O(n) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Insert a Range of Elements in a Set in C++ STL? M manjeet_04 Improve Article Tags : Misc C++ STL Practice Tags : CPPMiscSTL Similar Reads std::min_element in C++ The std::min_element() in C++ is an STL algorithm that is used to find the minimum element in a given range. This range can be array, vector, list or any other container. It is defined inside the <algorithm> header file. In this article, we will learn about the std::min_element() in C++.Exampl 4 min read max_element in C++ STL The std::max_element() in C++ is an STL algorithm that is used to find the maximum element in the given range. It is defined inside the <algorithm> header file. In this article, we will learn how to find the maximum element in the range using std::max_element() in C++.Example:C++// C++ program 4 min read list::empty() and list::size() in C++ STL Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::empty()empty() function is used to check if 3 min read How to Insert a Range of Elements in a Set in C++ STL? Prerequisites: Set in C++ Sets in C++ are a type of associative container in which each element has to be unique because the value of the element identifies it. The values are stored in a specific sorted order i.e. either ascending or descending. Syntax: set<datatype> set_name; Some Basic Func 2 min read set max_size() function in C++ STL The set::max_size() is a built-in function in C++ STL which returns the maximum number of elements a set container can hold. Syntax: set_name.max_size() Parameters: This function does not accept any parameters. Return Value: This function returns the maximum number of elements a set container can ho 1 min read smatch max_size() function in C++ STL The smatch::max_size() is a built-in function in C++ STL which returns the maximum number of elements in the match_results object that can be held by the smatch container. Syntax: smatch_name.max_size() Parameters: This function does not accept any parameters. Return value: This function returns the 1 min read Like