std::max in C++

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

  1. It compares the two numbers passed in its arguments and returns the larger 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 pre-defined by the user, and then passed as argument in std::max().
  3. It is also useful if we want to find the largest 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& max (const T& a, const T& b);
    
    Here, a and b are the numbers to be compared.
    Returns: Larger of the two values.
    

    CPP

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Python3

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python 3 program to
    # demonstrate the use of std::max
      
    # Comparing ASCII
    # values of a and b
    print(max('a','b'))
      
    # Returns the first
    # one if both the numbers
    # are same
    print(max(7, 7))
      
    # This code is contributed by 
    # Smitha Dinesh Semwal

    chevron_right

    
    


    Output:

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

    Syntax:

    template
    constexpr const T& max (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 f
    irst 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: Larger of the two values.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    28
    7
    
  3. For finding the maximum element in a list:

    Syntax:

    template 
    constexpr T max (initializer_list il, Compare comp);
    
    Here, comp is optional and can be skipped.
    il: An initializer_list object.
    Returns: Largest of all the values.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    10
    

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.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.