C++ Algorithm Max Function

C++ Algorithm max function can be used in following 3 ways:

  • It compares the two values passed in its arguments and returns the larger between them . If both are equal, then it returns the first one.
  • It also compares the two values using a binary function which is defined by the user, and then passed as an argument in std::max.
  • It is also used to find the largest element in a given list , and it returns the first one if there are more than one are largest in the list.

Elements are compared using operator < for the first version or using the given binary comparison function comp for the second version.

Syntax

Example

default (1)  template <class T> const T& max (const T& a, const T& b);     //until C++ 11

custom (2)    template <class T, class Compare>

                         const T& max (const T& a, const T& b, Compare comp);     //until C++ 11

default (1)    template <class T> const T& max (const T& a, const T& b);   //until C++ 14

custom (2)     template <class T, class Compare>

                   const T& max (const T& a, const T& b, Compare comp);            //until C++ 14

initializer list (3)    template <class T> T max (initializer_list<T> il);

                                 template <class T, class Compare>

                                T max (initializer_list<T> il, Compare comp);              //until C++ 14

default (1)   template <class T> constexpr const T& max (const T& a, const T& b);								     //since C++ 14

										       //since C++ 14

custom (2)    template <class T, class Compare>

                     constexp const T& max(const T& a, const T& b, Compare comp);

								    // since C++ 14

initializer list (3)   template <class T> constexpr T max (initializer_list<T> il);

                                template <class T, class Compare>

                                  constexpr T max (initializer_list<T> il, Compare comp);

								     //since C++ 14

Parameter

a : First value to compare.

b : Second value to compare.

comp : A user-defined binary predicate function that accepts two arguments and returns true if the two arguments are in order otherwise it returns false. It follows the strict weak ordering to order the elements.

il : An initializer_list with the values to compare.

Return value

It returns the maximum of a and b. If the values are equivalent, it returns a.

Returns the largest value in il. If several values are equivalent to the maximum, returns the left most such value.

Complexity

Complexity is linear in one less than the number of elements compared.

Exceptions

This function throws an exception if any comparison throws an exception.

Note: The invalid parameters cause an undefined behavior.

Example 1

Let's see the simple example to demonstrate the use of max:

Example

#include <algorithm>

#include <iostream>

#include <string>

using namespace std;

 

int main()

{

    cout << "larger of 1 and 9999: " << std::max(1, 9999) << '\n'

              << "larger of 'a', and 'b': " << max('a', 'b') << '\n'

              << "longest of \"foo\", \"bar\", and \"hello\": " <<

                  max( { "foo", "bar", "hello" },

                            [](const string& s1, const string& s2) {

                                 return s1.size() < s2.size();

                             }) << '\n';

    return 0;

}

Output:

Output

larger of 1 and 9999: 9999

larger of 'a', and 'b': b

longest of "foo", "bar", and "hello": hello

Example 2

Let's see another simple example to demonstrate the use of max using default version:

Example

#include <iostream>     // std::cout

#include <algorithm>    // std::max

using namespace std;

int main () {

  cout << "max(1,2)==" << max(1,2) << '\n';

  cout << "max(2,1)==" << max(2,1) << '\n';

  cout << "max('a','z')==" << max('a','z') << '\n';

  cout << "max(3.14,2.73)==" << max(3.14,2.73) << '\n';

  return 0;

}

Output:

Output

max(1,2)==2

max(2,1)==2

max('a','z')==z

max(3.14,2.73)==3.14

Example 3

Let's see another simple example to demonstrate the use of max using comparison function:

Example

#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 << max(a,b,comp) << "\n"; 

  

    // Returns the first one if both the numbers 

    // are same 

    cout << max(7,7,comp); 

  

return 0; 

}

Output:

Example 4

Let's see a simple example to find the maximum element in the list:

Example

#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 << "Maximum element is: "<< max({1, 2, 3, 4, 5, 10, -1, 7},comp) << "\n"; 

  

return 0; 

}

Output:

Output

Maximum element is: 10

Input Required

This code uses input(). Please provide values below: