Open In App

std::min in C++

Last Updated : 26 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The std::min() is used to find the minimum element among the given elements. It is the built-in function of C++ STL defined inside <algorithm> header file.

In this article, we will learn how to use std::min() function in C++. 

std::min() Function Signature

template< class T, class Compare >
const T& min (const T& a, const T& b, Compare comp);

template< class T, class Compare >
T min (std::initializer_list<T> ilist, Compare comp);

The std::min() can be used in three different ways to:

Find Minimum Among Two Elements

We can use std::min() function to find the smaller elements between the two elements. It uses < operator for comparison.

Syntax

min(a , b);

Parameters

  • a: First value
  • b: Second value

Return Value

  • Returns the smaller of the two values.
  • If both are equal, returns the first value.

Example

C++
// C++ program to find the smaller number among
// two numbers using std::min()
#include <bits/stdc++.h>
using namespace std;

int main() {
    int a = 8, b = 91;

    // Finding minimum among a and b
    cout << min(a, b) << endl;
    return 0;
}

Output
8

Time complexity: O(1)
Auxiliary Space: O(1)

Find the Minimum Among the Multiple Values

The std::min() function can also find the minimum value between more than two values. To achieve this, we have to pass the values in an initializer list, enclosed in {} and separated by commas. It uses < operator with to compare all the values pairwise.

Syntax

min({v1, v2, v3…});

Parameters

  • v1, v2, v3…: List of values.

Return Value

  • Returns the Smallest value among the given lists.
  • If all are equal, return the first value.

Example

C++
// C++ program to demonstrate how to find the
// smallest number among the list of numbers
// using std::min()
#include<bits/stdc++.h> 
using namespace std; 

int main() { 
  
  // Finding the smallest of all the numbers 
  cout << min({1, 2, 3, 4, 5, 10, -1, 7})
    << endl; 
  
  return 0; 
}

Output
-1

Time Complexity: O(n), where n is the number of elements.
Auxiliary Space: O(1)

Find Minimum Using Custom Comparator

The std::min() function also supports the use of custom comparator function to change the way of comparison. It is by default set to find the minimum element but we can also change it to perform any other desired comparison. It is especially useful to compare values of user defined data type.

Syntax

std::min (a, b, comp)

std::min ({v1, v2, v3…}, comp)

where, comp is the comparator function, lambda expression or even functors. This comparator function should follow these rules:

  • Return value should be of bool or any bool convertible type.
  • Should take two arguments.
  • Should not modify the arguments.

It returns true if a is smaller than b. False otherwise.

Example

C++
// C++ program to find the smaller value among
// two values of user defined type using
// std::min() and custom comparator
#include <bits/stdc++.h>
using namespace std;

class St {
  public:
  int sno;
  string name;
  St(int val, string s): sno(val), name(s) {}
};

// Comparator that works for data type A
bool comp(const St& a, const St& b) {
  return a.sno < b.sno;
}

int main() {
    St a(8, "Divesh");
  	St b(91, "Rohan");

    // std::min() with custom comparator
  	auto smaller = min(a, b, comp);
    cout << smaller.sno << " " << smaller.name
      << endl;
    return 0;
}

Output
8 Divesh

Time complexity: O(1)
Auxiliary Space: O(1)





Next Article
Article Tags :
Practice Tags :

Similar Reads