×

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

C++ Socket Programming

In this world, computer networking has become very important for sharing of data. Every good programmer has some knowledge about computer networking. Socket programming is one of the critical topics...

6 minutes read.

Functors in C++

Functors are not a very popular thing among beginner or intermediate-level programmers. But this thing is very useful and helpful. The name functor suggests us some similarities with function. It...

3 minutes read.

Is it fine to write void main() or main() in C/C++?

In C programming language: The default function return type in the C programming language is "int," which implies that main() will always return an integer value. In C, the void main()...

3 minutes read.

Program to arrange an array in alternate positive and negative numbers

Let’s say, there is given an array arr, arrange the array in such a way that every positive number is followed by a negative number. If there are extra positive...

4 minutes read.

Virtual Function Vs Pure Virtual Function

Virtual activity is a member function defined in the foundation phase that can be redefined by acquired classes. Let's have a look at an example: #include <iostream>   #include <bits/stdc++.h> #include <stdlib> using namespace std;   class Base   {    ...

5 minutes read.

C++ String

A string is a collection of characters. C++ programming language supports both C string as well as standard C++ library string. In C++, string is an object of std::string class. C Style String The C style...

5 minutes read.

Default Constructor

C++ Default Constructor A default constructor is such constructor which does not take any parameter. A constructor initializes the data member when a class object created. If a programmer does not create any...

2 minutes read.

Reverse an Array in C++

The many approaches to reverse an array in the C++ programming language will be discussed in this section. The term "reverse of an array" refers to changing the order of...

8 minutes read.

Returning a Function Pointer from a Function in C/C++

Pointers to functions can be used in the C programming language just like standard data pointers such as "int *," "char *," etc. The following is a basic example of a...

3 minutes read.

C++ Inheritance

What do you mean by Inheritance ? The ability to define new classes based on existing classes in order to reuse and organise code is referred to as inheritance. Single inheritance...

7 minutes read.

Remove duplicates from sorted array in C++

Remove duplicates from the sorted array in C++. This same process, due to a sorted array, is to erase the redundant components from the array. Examples: Input  : arr[] = {2, 2, 2,...

4 minutes read.

Program that produces different results in C and C++

Introduction: There are many such programs that compile run both in C and C++ but give different outcomes when compiled by the C and C++ compilers. There are a variety of such...

6 minutes read.

Parameterize Constructor

C++ Parameterized Constructor A constructor having parameters is known as parameterize constructor. Parameterize constructor is used to assign different values. Syntax: className(data-type argument){   // Constructor definition   }   className(data-type argument, data-type argument){   // Constructor definition   } A parameterized constructor can be passed values to constructor function in two ways: 1)...

1 minute read.

OOPs Concepts in C++

C++ Object-Oriented Programming Concepts C++ uses the concept of object-oriented programming. Object Oriented Programming has some prominent features: Object Class Data abstraction Encapsulation Polymorphism Inheritance Message passing Object An object is the basic unit...

2 minutes read.

Multiset in C++

Introduction Multisets are part of the C++ STL, or Standard Template Library. In C++, a multiset is a set of associative containers that hold ordered items. Items in a multiset can...

10 minutes read.

C++ Expressions

C ++ equations are made up of operators, constants and variables arranged according to language rules. It may also include function calls that give results. To calculate the value, the...

4 minutes read.

Dynamic Memory Allocation in C++

In some programming situations, the number of data items changes as the program is running, which is known as dynamic data or input. Consider a real-world situation where a program...

3 minutes read.

C ++ Program: Alphabet Triangle and Number Triangle

Alphabet Triangle and Number Triangle An alphabet triangle is a triangle that typically looks like a pyramid or other triangles like an isosceles triangle, a right-angled triangle consisting of similar or...

4 minutes read.

Difference between OOP and POP in C++

Object-Oriented Programming (OOP) Prioritizes data over methods (functions) and treats data as an essential component of program development.  OOP prohibits the free flow of data throughout the system. Tighter ties to data manipulation...

4 minutes read.

Copy constructor

Let's start by learning what a constructor is before diving into the copy constructor in C++. What is a constructor? A constructor is a particular type of class member function that configures the objects of...

7 minutes read.