std::min in C++ Last Updated : 19 May, 2025 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.Let's take the simplest example to demonstrate how the min() function works: C++ #include <bits/stdc++.h> using namespace std; int main() { cout << min(3,7); return 0; } Output3min() Function Signature C++ 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() function can be used in three different ways to:Table of ContentFind Minimum Among Two ElementsFind the Minimum Among the Multiple ValuesFind Minimum Using Custom ComparatorFind Minimum Among Two ElementsWe can use std::min() function to find the smaller elements between the two elements. It uses < operator for comparison. C++ min(a , b); Here,a: First valueb: Second valueThis function returns the smaller of the two values. If both are equal, returns the first value.Example C++ #include <bits/stdc++.h> using namespace std; int main() { int a = 8, b = 91; // Finding minimum among a and b cout << min(a, b); return 0; } Output8 Time complexity: O(1)Auxiliary Space: O(1)Find the Minimum Among the Multiple ValuesThe 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. C++ min({v1, v2, v3...}); Here,v1, v2, v3...: List of values.This function returns the smallest value among the given lists. If all are equal, return the first value.Example C++ #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}); return 0; } Output-1Find Minimum Using Custom ComparatorThe 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. C++ 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++ #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; return 0; } Output8 Divesh Comment More infoAdvertise with us Next Article std::min in C++ M Mrigendra Singh Improve Article Tags : Misc C++ STL cpp-algorithm-library Practice Tags : CPPMiscSTL Similar Reads C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to 5 min read SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e 7 min read TCP/IP Model The TCP/IP model (Transmission Control Protocol/Internet Protocol) is a four-layer networking framework that enables reliable communication between devices over interconnected networks. It provides a standardized set of protocols for transmitting data across interconnected networks, ensuring efficie 7 min read Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th 5 min read Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br 14 min read Unified Modeling Language (UML) Diagrams Unified Modeling Language (UML) is a general-purpose modeling language. The main aim of UML is to define a standard way to visualize the way a system has been designed. It is quite similar to blueprints used in other fields of engineering. UML is not a programming language, it is rather a visual lan 14 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Waterfall Model - Software Engineering The Waterfall Model is a Traditional Software Development Methodology. It was first introduced by Winston W. Royce in 1970. It is a linear and sequential approach to software development that consists of several phases. This classical waterfall model is simple and idealistic. It is important because 13 min read Python Lists In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s 6 min read Second Largest Element in an Array Given an array of positive integers arr[] of size n, the task is to find second largest distinct element in the array.Note: If the second largest element does not exist, return -1. Examples:Input: arr[] = [12, 35, 1, 10, 34, 1]Output: 34Explanation: The largest element of the array is 35 and the sec 14 min read Like