std::min in C++
std::min in C++
std::min is specified in the program code, used to calculate the lowest amount that has been transferred. When there's more of someone who returns first of them.
It's used in 3 ways:
- This identifies two numbers carried through in its statements and returns the smaller of a two, and if both are equivalent, then returns a first.
- This can use a binary method to evaluate the various digits specified by the client and passed in std::min() as a statement.
- Also, it is beneficial if we want to figure the smallest value in a provided series, and if there is and over one in the list, it returns the first one.
Its different phases as set out below are:
For comparing elements using < :
Syntax:
Constexpr const template T & min (const T & s, const T & r); The numbers s and r are for comparison. Returns: The two points are smaller.
Example:
// C++ program to demonstrate the use of std::min #include <iostream> #include <algorithm> using namespace std; int main() { int s = 4; int k = 6; cout << std::min(s, k) << "\n"; // Returns the first one if both the numbers // are same cout << std::min(8, 8); return 0; }
Output:

For comparing elements using a pre-defined function:
Syntax:
template T&min constexpr const (const T&a, const T&b, compare comp); Here, the numbers a and b are comparable. Comp: Binary function which takes two Type T values as statements, And the convertible value returns to bool. The returned value indicates if the The factor passed as the first statement is deemed less than that of the second argument. A feature won't alter any of its statements. It can be either a point of function or an object of the feature. Returns: The two points are smaller.
Example:
// C++ program to demonstrate the use of std::min #include <iostream> #include <algorithm> using namespace std; // Defining the binary function bool comp(int i, int k) { return (i < k); } int main() { int i = 2; int k = 4; cout << std::min(i, k, comp) << "\n"; // Returns the first one if both the numbers // are same cout << std::min(5, 5, comp); return 0; }
Output:

For finding the smallest element in a list:
Syntax:
template Constexpr T min (compare it with the initializer list); Comp is optional and is skippable. Il: An object initialized by list. Returns: Of all values, the smallest.
Example:
/* C++ program to prove std use::min */ #include <iostream> #include <algorithm> #include <vector> using namespace std; // Defining the binary function bool comp(int x, int y) { return (x < y); } int main() { // Finding the smallest of all the numbers cout << std::min({ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, -1, 11 }, comp) << "\n"; return 0; }
Output:
