Signup/Sign In

Minimum and Maximum operations in STL

Following are the functions that we will be covering :

  • max Method
  • max_element Method
  • min Method
  • min_element Method
  • minmax Method
  • minmax_element Method
  • lixicographically_compare Method
  • next_permutation Method
  • prev_permutation Method

max and min Method

max method's syntax is: max(object_type a, object_type b, compare_function)

This method returns the larger element of a and b. compare_function can be omitted. If there is no compare_function used in max() then elements are compared with operator > by default. compare_function is used to determine which one of the object is larger when the objects a and b are non numeric type.


min method's syntax is: min(object_type a, object_type b, compare_function)

This method returns the smaller element of a and b. compare_function can be omitted. If there is no compare_function used in min() then elements are compared with operator < by default. compare_function is used to determine which one of the object is smaller when the objects a and b are non numeric type.

Following is an example to demonstrate the usage of max() and min() methods.

#include<iostream>
#include<algorithm>

using namespace std;

/*compare function for strings*/
bool myMaxCompare(string a, string b)
{
    return (a.size() > b.size());
}

bool myMinCompare(string a, string b)
{
    return (a.size() > b.size());
}

int main()
{
    int x=4, y=5;
    
    cout << max(x,y);   // prints 5
    cout << min(x,y);   // prints 4
    
    cout << max(2.312, 5.434);     // prints 5.434
    cout << min(2.312, 5.434);     // prints 2.312
    
    string s = "smaller srting";
    string t = "longer string---";
    
    string s1 = max(s, t, myMaxCompare);
    cout<< s1 <<endl;  // prints longer string---
    
    string s1 = min(s, t, myMinCompare);
    cout<< s1 <<endl;  // prints smaller string
}

max_element and min_element Method

max_element method's syntax is:

max_element(iterator first, iterator last, compare_function)

This method returns the largest element in the range [first, last]. compare_function can be omitted. If there is no compare_function used in max_element() then elements are compared with operator > by default. compare_function is used to determine which one of the object is larger when the objects a and b are non numeric types.


min_element method's syntax is:

min_element(iterator first, iterator last, compare_function)

This method returns the smaller element in the range [first, last]. compare_function can be omitted. If there is no compare_function used in min_element() then elements are compared with operator < by default. compare_function is used to determine which one of the object is smaller when the objects a and b are non numeric types.

Following is an example to demonstrate usage of max_element() and min_element() method.

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

bool myMaxCompare(int a, int b)
{
    return (a < b);
}

bool myMinCompare(int a, int b)
{
    return (a < b);
}

int main()
{
    int values[] = { 1,5,4,9,8,10,6,5,1};
    vector<int> v(values,values+9);

    cout<< *max_element(v.begin(), v.end());
    /* prints 10 */
    
    cout<< *min_element(v.begin(), v.end());
    /* prints 1 */

    /* using mycompare function */
    cout<< *max_element(v.begin(), v.end(), myMaxCompare);
    /* prints 10 */
    
    cout<< *min_element(v.begin(), v.end(), myMinCompare);
    /* prints 1 */
}

lexicographical_compare Method

The syntax for this method is :

lexicographical_compare(iterator first1, iterator last1, iterator first2, iterator last2)

It compares the ranges [first1,last1] and [first2,last2] and returns true if the first range is lexicographically smaller than the later one.

A custom compare function can be defined and used when we want to define how the elements are to be compared. Following is the syntax of that variant of this method.

lexicographical_compare(iterator first1, iterator last1, iterator first2, iterator last2, bool compare_function)

Following is a program to demonstrate its usage :

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

bool myoperator(char a , char b)
{
    return a > b;

}

int main()
{
    char s[] = "nkvaio";
    char x[] = "xyzabc";
    cout >> lexicographical_compare(s, s+6, x, x+6, myoperator);
    /*  prints 0 , Boolean false , since a[4] is not less than b[4]  */
}