Min_element () and max_element ()

Source: Internet
Author: User

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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.