×

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::min() function with example in C++ STL

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

C++ STL std::min() function

min() function is a library function of algorithm header, it is used to find the smallest value from given two values, it accepts two values and returns the smallest value and if both the values are the same it returns the first value.

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

Syntax

Syntax of std::min() function

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

Parameter(s)

const T& a, const T& b – values to be compared.

Return value

T – it returns the smallest value of type T.

Sample Input and Output

Input:
int a = 10;
int b = 20;
    
//finding smallest value
cout << min(a,b) << endl;
    
Output:
10

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

In this example, we are going to find the smallest values from given values of different types.

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

int main()
{
    cout << "min(10,20)        : " << min(10, 20) << endl;
    cout << "min(10.23f,20.12f): " << min(10.23f, 20.12f) << endl;
    cout << "min(-10,-20)      : " << min(-10, -20) << endl;
    cout << "min('A','a')      : " << min('A', 'a') << endl;
    cout << "min('A','Z')      : " << min('A', 'Z') << endl;
    cout << "min(10,10)        : " << min(10, 10) << endl;
	
    return 0;
}

Output

min(10,20)        : 10
min(10.23f,20.12f): 10.23
min(-10,-20)      : -20
min('A','a')      : A
min('A','Z')      : A
min(10,10)        : 10

Reference: C++ std::min()

Comments and Discussions!

Load comments ↻





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