Open In App

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

Improve
Improve
Like Article
Like
Save
Share
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.

  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. 

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;
     
}


  1. 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)

  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;
     
}


  1. 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)



Last Updated : 21 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads