Home » C++ STL

std::minmax() function with example in C++ STL

C++ STL | std::minmax() function: Here, we are going to learn about the minmax() function of algorithm header in C++ STL with example.
Submitted by IncludeHelp, on May 20, 2019

C++ STL std::minmax() function

minmax() function is a library function of algorithm header, it is used to find the smallest and largest values, it accepts two values and returns a pair of the smallest and largest values, the first element of the pair contains the smallest value and the second element of the pair contains the largest value.

Note:To use minmax() function – include <algorithm> header or you can simple use <bits/stdc++.h> header file.

Syntax of std::minmax() function

    std::minmax(const T& a, const T& b);

Parameter(s): const T& a, const T& b – values to be compared.

Return value: pair – it returns the pair of the smallest and the largest values.

Example:

    Input:
    int a = 10;
    int b = 20;
    
    //finding pair of smallest and largest numbet
    auto result = minmax(a, b);

    cout << result.first << endl;
    cout << result.second << endl;
    
    Output:
    10
    20

C++ STL program to demonstrate use of std::minmax() function

In this program, we have two integer variables and finding the smallest and the largest values.

//C++ STL program to demonstrate use of 
//std::minmax() function
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int a = -10;
    int b = -20;

    //finding pair of smallest and largest numbet
    auto result = minmax(a, b);

    //printing the smallest and largest values
    cout << "smallest number is: " << result.first << endl;
    cout << "largest number is: " << result.second << endl;

    return 0;
}

Output

smallest number is: -20
largest number is: -10

Reference: C++ std::minmax()






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.