.\" ident @(#)min_element.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH min_element 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2min_element\fP \ - Finds the minimum value in a range. .SH SYNOPSIS .br #include .br template .br ForwardIterator .RE .RS 1 min_element(ForwardIterator first, ForwardIterator last); .RE .RS 0 .br template .br InputIterator .RE .RS 1 min_element(ForwardIterator first, ForwardIterator last, .RE .RS 12 Compare comp); .SH DESCRIPTION The min_element algorithm returns an iterator that denotes the minimum element in a sequence. If the sequence contains more than one copy of the minimum element, the iterator points to the first occurrence of the element. In the second version of the function, the optional argument \f2comp\fP defines a comparison function that can be used in place of the default \f2operator<\fP. Algorithm min_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!(*j < *i)\fP or \f2comp(*j, *i) == false. \fP .SH COMPLEXITY min_element performs exactly \f2max((last - first) - 1, 0)\fP applications of the corresponding comparisons. .SH EXAMPLE .RE .RS 0 // .br // max_elem.cpp .br // .RE .RS 1 #include .br #include .br #include .RE .RS 0 using namespace std; .br .br 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 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, max_element, min