stlab.adobe.com Adobe Systems Incorporated

max_element

algorithms.gif
function.gif
Category: algorithms Component type: function

Prototype

Max_element is an overloaded name; there are actually two max_element functions.

template <class ForwardIterator>
ForwardIterator max_element(ForwardIterator first, ForwardIterator last);

template <class ForwardIterator, class BinaryPredicate>
ForwardIterator max_element(ForwardIterator first, ForwardIterator last,
                            BinaryPredicate comp);

Description

Max_element finds the largest element in the range [first, last). It returns the first iterator i in [first, last) such that no other iterator in [first, last) points to a value greater than *i. The return value is last if and only if [first, last) is an empty range.

The two versions of max_element differ in how they define whether one element is less than another. The first version compares objects using operator<, and the second compares objects using a functors comp.

The first version of max_element returns the first iterator i in [first, last) such that, for every iterator j in [first, last), *i < *j is false. The second version returns the first iterator i in [first, last) such that, for every iterator j in [first, last), comp(*i, *j) is false.

Definition

Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.

Requirements on types

For the first version:

For the second version:

  • ForwardIterator is a model of ForwardIterator.
  • BinaryPredicate is a model of BinaryPredicate.
  • ForwardIterator's value type is convertible to BinaryPredicate's first argument type and second argument type.

Preconditions

  • [first, last) is a valid range.

Complexity

Linear. Zero comparisons if [first, last) is an empty range, otherwise exactly (last - first) - 1 comparisons.

Example

int main()
{
  List<int> L;
  generate_n(front_insert_iterator(L), 1000, rand);
  
  list<int>::const_iterator it = max_element(L.begin(), L.end());
  cout << "The largest element is " << *it << endl;
}

Notes

See also

min, max, min_element, LessThanComparable, sort, nth_element

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google