Standard C++ Library Class Reference

Description
The max_element algorithm returns an iterator that denotes the maximum element in a
sequence. If the sequence contains more than one copy of the element, the iterator points to its
first occurrence. The optional argument comp defines a comparison function that can be used in
place of the default operator<. This function can be used with all the datatypes provided by the
standard library.
Algorithm max_element returns the first iterator i in the range [first, last) such that for any
iterator j in the same range the following corresponding conditions hold:
!(*i < *j)
or
comp(*i, *j) == false.
Complexity
Exactly max((last - first) - 1, 0) applications of the corresponding comparisons are done for
max_element.
Example
//
// max_elem.cpp
//
#include <algorithm>
#include <vector>
#include <iostream.h>
int main(void)
{
typedef vector<int>::iterator iterator;
int d1[5] = {1,3,5,32,64};
// set up vector
vector<int> v1(d1,d1 + 5);
// find the largest element in the vector
iterator it1 = max_element(v1.begin(), v1.end());
// it1 = v1.begin() + 4
// find the largest element in the range from
// the beginning of the vector to the 2nd to last
iterator it2 = max_element(v1.begin(), v1.end()-1,
less<int>());
// it2 = v1.begin() + 3