×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

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

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.

Sample Input and Output

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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.