C++ Algorithm max()14 Dec 2025 | 3 min read 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 TopicCpp algorithm next_permutation function |
algorithm nth_element() function
C++ Algorithm nth_element() C++ Algorithm nth_element() function is used to sort the elements between the first and nth element in ascending order and element between nth and last are not sorted. However, no element in between nth and last is smaller than an element between first and...
6 min read
algorithm random_shuffle() function
C++ Algorithm random_shuffle() C++ Algorithm random_shuffle() reorders the elements of a range by putting them at random places. The first version uses an internal random number generator and the second version uses a random number generator which is a special kind of function object that is explicitly...
4 min read
algorithm rotate() function
C++ Algorithm rotate() C++ Algorithm rotate() function is used to rotate the order of the elements within a range [first, last). The sequence will start at the element in the middle of the source sequence and the last element will be followed by first. middle to the elements between...
7 min read
algorithm find_if_not() function
C++ Algorithm Function find_if_not() C++ Algorithm find_if_not()function returns the value of the first element in the range for which the pred value is false otherwise the last element of the range is given. Syntax template <class InputIterator, class UnaryPredicate> InputIterator find_if_not (InputIterator first, InputIterator last, UnaryPredicate pred); Parameter first: It specifies the...
2 min read
Thread hardware_concurrency() Function in C++
Introduction: In the dynamic realm of programming, the ability to harness the full potential of hardware resources is a key aspect. Multithreading, the simultaneous execution of multiple threads, plays a crucial role in achieving parallelism and enhancing performance. C++, being a versatile programming language, offers the...
7 min read
algorithm reverse() function
C++ Algorithm reverse() C++ Algorithm reverse() function is used to reverse the order of the elements within a range [first, last). Syntax template <class BidirectionalIterator> void reverse (BidirectionalIterator first, BidirectionalIterator last); Note: BidirectionalIterator is an iterator which is used to access any elements of a container in both forward and...
4 min read
algorithm iter_swap() function
C++ Algorithm iter_swap() C++ Algorithm iter_swap() exchanges the elements pointed to by two iterators a and b. Syntax template <class ForwardIterator1, class ForwardIterator2> void iter_swap (ForwardIterator1 a, ForwardIterator2 b); Parameter a: One of the forward iterator whose value is to be exchanged. b: Second of the forward iterator whose value is...
7 min read
algorithm find_first_of() function
C++ Algorithm Function find_first_of() C++ Algorithm find_first_of() function compares the values stored in two containers i.e [first1, last1) and [first2, last2). If an element similar to the one in the range [first2, last2) is found in [first1, last1) then an iterator to that element is returned by...
2 min read
algorithm is_sorted_until() function
C++ Algorithm is_sorted_until() C++ Algorithm is_sorted_until() function is used to find first unsorted element in the range. It means it returns an iterator to the first element in the range [first, last) which does not follow an ascending order. The elements are compared using operator < for...
4 min read
algorithm partition_point() function
C++ Algorithm partition_point() C++ Algorithm partition_point() function is used to return the first element in the given range for which pred is not true. The elements are sorted in a way that the elements which satisfy the condition come before those who do not satisfy. Syntax template <class...
5 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India