Home » C++ STL

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 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.

Example:

    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

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.