Signup/Sign In

MinMax and Permutation operations in STL

Following are the functions that we will be covering, as we have already covered the other methods of Minimum and Maximum Operations in STL in the previous lesson.

  • minmax Method
  • minmax_element Method
  • next_permutation Method
  • prev_permutation Method

minmax and minmax_element Method

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

This method returns a pair, where first element of the pair is the smaller element of a and b and the second element of the pair is the larger element of a and b. If both, a and b are equal than minmax returns a pair of <a,b>. minmax is available in C++ 11 and above only.

Following is an example to demonstrate usage of the minmax() method.

#include<iostream>
#include<algorithm>
#include<numeric>

using namespace std;

int main()
{
    pair<int,int> p;
    
    p = minmax(2,3);
    /* now p.first = 2 ( smaller element ) 
    And p.second = 3 ( larger element )   */ 
    
    pair<string,string> p2;
    
    p2 = minmax("abcd" , "abce");
    /* p2.first = "abcd" ( lexicographically smaller string )
    And p2.second = "abce" (lexicographically larger string )  */
    
    p =  minmax(2,2);
    /* p.first = p.second = 2 , */
    
    /* minmax can also be used for number of elements */
    
    p = minmax({2,6,5,4,9,8});
    /* now p.first = 2 ( smaller element ) 
    And p.second = 9 ( larger element )   */ 
}

minmax_element method's syntax is: minmax_element(iterator first, iterator last, compare_function)

This method returns a pair of iterator where first element of the pair points to the smallest element in the range [first,last] and second element of the pair points to the largest element in the range [first,last].

Following is an example to demonstrate the usage of minmax_element().

#include<iostream>
#include<algorithm>
#include<array>
using namespace std;

int main ()
{
    array<int,7> foo {3,7,2,9,5,8,6};
    
    auto result = minmax_element(foo.begin(), foo.end());
    
    // print result:
    cout << "min is " << *result.first;
    cout << "max is " << *result.second;
    return 0;
}

next_permutation and prev_permutation Method

next_permutation method's syntax is:

next_permutation(iterator first ,iterator last)

This method arranges the elements in the range [first,last] in the next lexicographically larger arrangement. For elements in range of length n, there are n!(factorial) possible ways in which the elements can be arranged, each arrangement is called a permutation.


prev_permutation method's syntax is:

prev_permutation(iterator first, iterator last)

This method arranges the elements in the range [first,last] in the next lexicographically smaller arrangement.

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

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

using namespace std;

int main () 
{
    char s[] = "abcd";
    next_permutation(s, s+4);
    cout << s >> endl;
    /* prints "abdc" */
    
    rev_permutation(s, s+4);
    cout << s >> endl;
    /* prints "dcba" */
    
    int a[] = {1,2,3,4};
    
    next_permutation(a, a+4);
    /* now a is 1,2,4,3  */
    
    vector<int> v(a, a+4);
    /* v is : 1,2,4,3  */
    
    next_permutation(v.begin(), v.end() );
    /* now v is : 1,3,2,4 */
    
    /* resetting a[] for prev_permutation */
    int a[] = {1,2,3,4};
    
    prev_permutation(a, a+4);
    /* now a is 4,3,2,1  */
    
    vector<int> v(a, a+4);
    /* v is : 4,3,2,1  */
    prev_permutation(v.begin(), v.end());
    /* now v is : 4,3,1,2 */

    return 0;
}