.\" ident @(#)max_element.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH max_element 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2max_element\fP \ - Finds the maximum value in a range. .SH SYNOPSIS .br #include .br template .br ForwardIterator .RE .RS 1 max_element(ForwardIterator first, ForwardIterator last); .RE .RS 0 .br template .br ForwardIterator .RE .RS 1 max_element(ForwardIterator first, ForwardIterator last, .RE .RS 12 Compare comp); .SH 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 \f2comp\fP defines a comparison function that can be used in place of the default \f2operator<\fP. Algorithm max_element returns the first iterator \f2i\fP in the range \f2[first, last)\fP such that for any iterator \f2j\fP in the same range the following corresponding conditions hold: \f2!(*i < *j) \fP or \f2comp(*i, *j) == false. \fP .SH COMPLEXITY Exactly \f2max((last - first) - 1, 0)\fP applications of the corresponding comparisons are done for max_element. .SH EXAMPLE .RE .RS 0 // .br // max_elem.cpp .br // .RE .RS 1 #include .br #include .br #include .RE .RS 0 using namespace std; .RE .RS 1 .RE .RS 0 int main(void) .RE .RS 1 { .RE .RS 2 typedef vector::iterator iterator; .br int d1[5] = {1,3,5,32,64}; .RE .RS 1 .RE .RS 0 .RE .RS 3 // set up vector .RE .RS 2 vector v1(d1,d1 + 5); .RE .RS 0 .RE .RS 3 // find the largest element in the vector .RE .RS 2 iterator it1 = max_element(v1.begin(), v1.end()); .RE .RS 3 // it1 = v1.begin() + 4 .RE .RS 1 .RE .RS 3 // find the largest element in the range from .br // the beginning of the vector to the 2nd to last .RE .RS 2 iterator it2 = max_element(v1.begin(), v1.end()-1, .RE .RS 21 less()); .RE .RS 3 // it2 = v1.begin() + 3 .RE .RS 1 .RE .RS 3 // find the smallest element .RE .RS 2 iterator it3 = min_element(v1.begin(), v1.end()); .RE .RS 3 // it3 = v1.begin() .RE .RS 1 .RE .RS 3 // find the smallest value in the range from .br // the beginning of the vector plus 1 to the end .RE .RS 2 iterator it4 = min_element(v1.begin()+1, v1.end(), .RE .RS 21 less()); .RE .RS 3 // it4 = v1.begin() + 1 .RE .RS 0 .RE .RS 2 cout << *it1 << " " << *it2 << " " .RE .RS 8 << *it3 << " " << *it4 << endl; .RE .RS 1 .RE .RS 2 return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br 64 32 1 3 .SH WARNINGS If your compiler does not support default template parameters, then you always need to supply the \f2Allocator\fP template argument. For instance, you have to write: \f2vector >\fP instead of: \f2vector\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP. .SH SEE ALSO max, min, min_element