Skip to content
Related Articles

Related Articles

std::min in C++
  • Difficulty Level : Easy
  • Last Updated : 24 Jul, 2017
GeeksforGeeks - Summer Carnival Banner

std::min is defined in the header file and is used to find out the smallest of the number passed to it. It returns the first of them, if there are more than one.
It can be used in following 3 manners:

  1. It compares the two numbers passed in its arguments and returns the smaller of the two, and if both are equal, then it returns the first one.
  2. It can also compare the two numbers using a binary function , which is defined by the user, and then passed as argument in std::min().
  3. It is also useful if we want to find the smallest element in a given list, and it returns the first one if there are more than one present in the list.

The three versions are as defined below:

  1. For comparing elements using < :

    Syntax:

    template  constexpr const T& min (const T& a, const T& b);
    
    a and b are the numbers to be compared.
    Returns: Smaller of the two values.
    




    // C++ program to demonstrate the use of std::min
    #include <iostream>
    #include <algorithm>
    using namespace std;
    int main()
    {
        int a = 5;
        int b = 7;
        cout << std::min(a, b) << "\n";
      
        // Returns the first one if both the numbers
        // are same
        cout << std::min(7, 7);
      
        return 0;
    }

    Output:

    5
    7
    
  2. For comparing elements using a pre-defined function:

    Syntax:



    template
    constexpr const T& min (const T& a, const T& b, Compare comp);
    
    Here, a and b are the numbers to be compared.
    
    comp: Binary function that accepts two values of type T as arguments,
    and returns a value convertible to bool. The value returned indicates whether the 
    element passed as first argument is considered less than the second.
    The function shall not modify any of its arguments.
    This can either be a function pointer or a function object.
    Returns: Smaller of the two values.
    




    // C++ program to demonstrate the use of std::min
    #include <iostream>
    #include <algorithm>
    using namespace std;
      
    // Defining the binary function
    bool comp(int a, int b)
    {
        return (a < b);
    }
    int main()
    {
        int a = 5;
        int b = 7;
        cout << std::min(a, b, comp) << "\n";
      
        // Returns the first one if both the numbers
        // are same
        cout << std::min(7, 7, comp);
      
        return 0;
    }

    Output:

    5
    7
    
  3. For finding the minimum element in a list:
    Syntax:
    template 
    constexpr T min (initializer_list il, Compare comp);
    
    comp is optional and can be skipped.
    il: An initializer_list object.
    Returns: Smallest of all the values.
    




    // C++ program to demonstrate the use of std::min
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
      
    // Defining the binary function
    bool comp(int a, int b)
    {
        return (a < b);
    }
    int main()
    {
      
        // Finding the smallest of all the numbers
        cout << std::min({ 1, 2, 3, 4, 5, 0, -1, 7 }, comp) << "\n";
      
        return 0;
    }

    Output:

    -1
    

Related Articles:

This article is contributed by Mrigendra Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :