next up previous index
Next: Determine the lexicographical Up: Sorting and related Previous: Heap operations

Finding minimum and maximum elements

Source code

Declaration

template <class T>
inline const T& min(const T& a, const T& b);

template <class T, class Compare>
inline const T& min(const T& a, const T& b, Compare comp);

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

template <class T, class Compare>
inline const T& max(const T& a, const T& b, Compare comp);

template <class InputIterator>
InputIterator min_element(InputIterator first, InputIterator last);

template <class InputIterator, class Compare>
InputIterator min_element(InputIterator first, InputIterator last,
                          Compare comp);

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

template <class InputIterator, class Compare>
InputIterator max_element(InputIterator first, InputIterator last,
                          Compare comp);

Description The min and max functions are passed two elements, and return the one that is smaller or larger, respectively.

The min_element and max_element functions return an iterator i referring to the minimum or maximum, respectively, of the elements in the range [ first, last) .

In the first function of each pair of functions, element comparisons are done using operator<, while in the second they are done using the function object comp .

Type requirements

Group Sorting and related operations

Time complexity Constant for the min and max functions, and linear for the min_element and max_element functions.

The number of comparisons performed by the min_element and max_element functions is the size of the range [ first, last) .

Space complexity Constant.

Mutative? No.

Details min and max return the first argument when the arguments are equal.

For min_element and max_element, the first minimum or maximum element, respectively, in the sequence is returned if there are other alternative elements.

Example Select here to view the source code for the example.

 

Implementation Select here to view source code (Copyright (C) 1994, Hewlett-Packard Company) for this algorithm.



Kenny Zalewski
Mon May 13 04:03:40 EDT 1996