The std in C means standard, referring to the standard library and the standard namespace. The standard library has a sub-library called, algorithm. The algorithm library has the function, max(), in overloaded forms. The max function returns the maximum of two values of the same type, or the maximum value in a list (of the same type of values).

In order to use the max() function of the C algorithm library, the program has to start with something with the algorithm library included like:

#include

#include

using namespace std;

There are four main overloaded forms of this max function and this article explains how to use them.

template<class T> constexpr const T& max(const T& a, const T& b)

This function takes two arguments of the same type and returns the bigger of both. If both are the same in value, the first occurrence is returned. The following program illustrates its use:

#include

#include

using namespace std;

    int main()


    {


        char ch = max(‘E’, ‘C’);


cout<<ch<<endl;

        return 0;


    }

The output is E.

template<class T, class Compare>

constexpr const T& max(const T& a, const T& b, Compare comp)

This overloaded function is similar to the above, but the programmer has defined his own comparison function. The above overloaded function uses the default comparison function. The definition of a comparison function, which does the same thing as the default comparison function is:

    bool compFn(char a, char b) {


        if (a < b)


            return true;


        else


            return false;


    }

It takes two values, which may be of a list, then its returns true, if the first one is less than the second one then it is false. In this function, ‘a’ is the first value and b is the second value. In the max() function syntax of the heading of this section, the first argument is ‘a’ and the second argument is b, while the third argument is the name of the comparison function without the parentheses and arguments.

Note that the type of the arguments of the comparison function is the same as the type of the arguments in the max() function.

The following program with a programmer defined function, has the same effect, as the above program:

#include

#include

using namespace std;

    bool compFn(char a, char b) {


        if (a < b)


            return true;


        else


            return false;


    }

    int main()


    {


        char ch = max(‘E’, ‘C’, compFn);


cout<<ch<<endl;

        return 0;


    }

The output is, E. If ‘C’ was typed before ‘E’ as arguments in the max() function, the output would still have been ‘E’.

template<class T> constexpr T max(initializer_list<T> t)

In C , an initializer_list is the array literal. This overloaded form returns the biggest value in an initializer_list. The following program illustrates this:

#include

#include

using namespace std;

    int main()


    {


        char ch = max({‘C’, ‘A’, ‘E’, ‘D’, ‘B’});


cout<<ch<<endl;


        return 0;


    }

The output is, E.

template<class T, class Compare>

constexpr T max(initializer_list<T> t, Compare comp)

This overloaded function is similar to the above code but it needs a comparison function. The above comparison function can be used as illustrated in the following program:

#include

#include

using namespace std;

    bool compFn(char a, char b) {


        if (a < b)


            return true;


        else


            return false;


    }

    int main()


    {


        char ch = max({‘C’, ‘A’, ‘E’, ‘D’, ‘B’}, compFn);


cout<<ch<<endl;


        return 0;


    }

The output is, E.

Custom max Function

The programmer can write his own max() function. The strategy is to actually do the comparison in the function.

Max of two Values

The following program shows how to determine the maximum value of two values:

    #include


    using namespace std;

    char max(char a, char b) {


        if (a > b)


            return a;


        else


            return b;


    }

    int main()


    {


        char ch = max(‘E’, ‘C’);


cout<<ch< b)


            return a;


        else


            return b;


    }

    int main()


    {


        char ch = max(‘C’, ‘E’);


cout<<ch<<endl;


        return 0;


    }

Maximum in a List

A custom program can also be written to find the maximum value in a list. The strategy is as follows:

The first element is assumed to be the maximum element in the list. If the first element is less than the next element, then the next element becomes the new maximum, else the first element remains the maximum. If the assumed maximum is less than the element after, the element after, becomes the new maximum, else, the old maximum remains. This comparison continues until the end of the list. The following program illustrates this:

    #include


    using namespace std;

    char max(char arr[], int size) {


        char maxVal = arr[0];

        for (int i=1; i<size; i ) {


            if (maxVal<arr[i])


maxVal = arr[i];


        }


        return maxVal;


    }

    int main()


    {


        char ar[] = {‘C’, ‘A’, ‘E’, ‘D’, ‘B’};


        char ch = max(ar, 5);


cout<<ch<<endl;


        return 0;


    }

The output is, E. The first statement in the custom max function, obtains the assumed max, in the form:

The next code segment is a for-loop. Inside the for-loop, is an if-construct, which does the comparison and assignment, as the scanning through the array continues.

In the C main function, the first statement declares the array, whose maximum element is required. The second statement calls the custom max function. The statement after, prints out the maximum value in the list.

Conclusion

Without writing a custom max function. In order to obtain the maximum value of two values, or from more than two values in a list, use an appropriate function below:

template<class T> constexpr const T& max(const T& a, const T& b)

template<class T, class Compare>

constexpr const T& max(const T& a, const T& b, Compare comp)

template<class T> constexpr T max(initializer_list<T> t)

template<class T, class Compare>

constexpr T max(initializer_list<T> t, Compare comp)

These overloaded functions are all in the algorithm library.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Chrysanthus-150×150.jpeg" height="112" src="data:image/svg xml,” width=”112″>

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.