Header file: # include <algorithm>
Header file category: min/MAX
Function: min_element () returns an iterator pointer pointing to the smallest element of [first, last) in a range; max_element () returns an iterator pointer, point to the smallest element in a range of [first, last. If more than one element meets this condition, the returned iterator points to the first element.
The min_element () function template has the following functions:
template <class ForwardIterator> ForwardIterator min_element ( ForwardIterator first, ForwardIterator last ){ if (first==last) return last; ForwardIterator smallest = first; while (++first!=last) if (*first<*smallest) // or: if (comp(*first,*smallest)) for version (2) smallest=first; return smallest;}
The max_element () function template has the following functions:
template <class ForwardIterator> ForwardIterator max_element ( ForwardIterator first, ForwardIterator last ){ if (first==last) return last; ForwardIterator largest = first; while (++first!=last) if (*largest<*first) // or: if (comp(*largest,*first)) for version (2) largest=first; return largest;}
Function template:
/***** Author: or_me ** contains two missing parts {/a c/} (OO )) required bytes must be ******* min_element/max_element example ***** July 22, July 2, 2014 *****/# include <cmath> # include <queue> # include <stack> # include <vector> # include <cstdio> # include <string> # include <cctype> # include <climits> # include <cstring> # include <cstdlib> # include <iostream> # include <algorithm> using namespace STD; bool myfn (int I, Int J) {return I <j;} struct myclass {bool operator () (int I, Int J) {return I <j ;}} myobj; int main () {system ("color 5B"); int myints [] = {, 9}; cout <"using default comparison: "<Endl; cout <" the smallest element is "<* min_element (myints, myints + 7) <Endl; cout <"the largest element is" <* max_element (myints, myints + 7) <Endl; cout <"using function myfn as COMP: "<Endl; cout <" the smallest element is "<* min_element (myints, myints + 7, myfn) <Endl; cout <"the largest element is" <* max_element (myints, myints + 7, myfn) <Endl; cout <"using object myobj as COMP: "<Endl; cout <" the smallest element is "<* min_element (myints, myints + 7, myobj) <Endl; cout <"the largest element is" <* max_element (myints, myints + 7, myobj) <Endl; return 0 ;}
Min_element () and max_element ()