C++ Algorithm max()C++ Algorithm max() function can be used in following 3 ways:
Elements are compared using operator < for the first version or using the given binary comparison function comp for the second version. SyntaxParametera: 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 valueIt 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. ComplexityComplexity is linear in one less than the number of elements compared. ExceptionsThis function throws an exception if any comparison throws an exception. Note: The invalid parameters cause an undefined behavior.Example 1Let's see the simple example to demonstrate the use of max(): Output: larger of 1 and 9999: 9999 larger of 'a', and 'b': b longest of "foo", "bar", and "hello": hello Example 2Let's see another simple example to demonstrate the use of max() using default version: Output: max(1,2)==2 max(2,1)==2 max('a','z')==z max(3.14,2.73)==3.14 Example 3Let's see another simple example to demonstrate the use of max() using comparison function: Output: 28 7 Example 4Let's see a simple example to find the maximum element in the list: Output: Maximum element is: 10
Next TopicC++ Algorithm
|