×

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:

std::min in C++

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:

std::min in C++

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:

std::min in C++

Related Topics

wcscpy(), wcslen(), wcscmp() Functions in C++

There are many built-in functions in C++ programming language which differentiate it from C programming language in most hardware-coded languages. We will now closely look into the applications of three...

4 minutes read.

Use of Inheritance in C++

What is inheritance in c++? Inheritance is fundamental in object-oriented programming (OOP) languages like C++. It allows a programmer to create a new class (called a derived class) that inherits the...

4 minutes read.

C++ Friend Functions

In C++, friends are special functions that are not a part of a class yet have access to its private and protected members. The friend keyword is used to declare...

4 minutes read.

C++ Static

What is the Static keyword? In C++, the keyword static is used to give an element some particular properties. Static elements are only given storage in the static storage region once...

4 minutes read.

Object in C++

In this article, we will learn about Object in C++. In short, an object is a stateful entity with behaviour. Data is referred to as state, and functionality is referred to...

3 minutes read.

C++ Inline function

A C++ function that extends in line when called is known as an inline function. It reduces function call overhead by having the compiler use the function code rather than...

4 minutes read.

C++ Object Class

C++ Object Class Overview: C++ is a high-level programming language and an object-oriented programming language. An object-oriented language always has some properties of classes and objects. In this article, we...

4 minutes read.

Templates in C++ vs Generics in Java

As the title suggests, there is no rivalry or there is no cut comparison between generics and templates in Java and C++, respectively. The main aim of this article is...

4 minutes read.

Exception Handling in C++ vs Java

Nowadays, exception handling is a feature found in virtually all object-oriented languages. We can also find this type of feature in Java and C++. The try-catch and block is required...

3 minutes read.

Principles of Object-Oriented Programming in C++

What is Object-Oriented Programming? Object-oriented programming is about creating obejcts that represent the real-world entity . In object-oriented programming, objects are created for the class. One of the main objectives of...

5 minutes read.

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

Splitting a string in C++

Any programming language must have the ability to work with string data. For programming needs, we sometimes need to separate string data. Many computer languages provide a split() method that...

4 minutes read.

How to create a directory or folder in C/C++?

A directory is to lists all files and subdirectories in a directory, set of the files will be kept in the directory, which is a location. A subdirectory is a...

5 minutes read.

C++ Bitwise XOR Operator

Exclusive OR is another name for the bitwise XOR operator. The ‘^’ is used to indicate it. It operates at the bit level of the operands, as the name implies....

4 minutes read.

Message Passing in C++

The act of sending and receiving information by an object is referred to as communication, and all communication between objects that takes place via message is known as message passing....

1 minute read.

Implementing the sets without C++ STL containers

Many practical features and tools in C++ support us in programming competitions. One of these parts is a set from the Standard Template Library (STL), which offers an effective way...

6 minutes read.

Assertions in C/C++

Assertions are the statements used to check presumptions which is made by the programmers. Example: Assertion is used to verify whether the malloc returned by the pointer is NULL or not. For...

4 minutes read.

How to implement map in C++

Part of the C++ STL is maps (Standard Template Library). Maps are associative containers that hold sorted key-value pairs, where each key is distinct and may only be added or...

4 minutes read.

Learn C++ Tutorial

C++ Introduction C++ is an object-oriented programming language. It was developed by Bjarne Stroustrup at AT&T Bell Laboratories. It is superset (extension) of C programming language. Depending upon features supported by programming...

10 minutes read.

User-defined literals in C++

Introduction to User-Defined Literals: User-defined literals, introduced in C++11, are a way to extend the C++ language to allow users to define their literal suffixes and the corresponding behavior. These suffixes...

7 minutes read.