⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 max_8656.htm

📁 ARM编辑、编译软件
💻 HTM
字号:
<HTML><TITLE>max_element</TITLE><BODY>
<A HREF="ref.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the Class Reference home page.</STRONG></P>
<P>&copy;Copyright 1996 Rogue Wave Software</P>
<H2>max_element</H2>
<HR><PRE>     Algorithm</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>Finds maximum value in a range.</P>
<H3>Contents</H3>
<UL>
<A HREF="#Synopsis"><LI>Synopsis</LI></A>
<A HREF="#Description"><LI>Description</LI></A>
<A HREF="#Complexity"><LI>Complexity</LI></A>
<A HREF="#Example"><LI>Example</LI></A>
<A HREF="#Warning"><LI>Warning</LI></A>
<A HREF="#See Also"><LI>See Also</LI></A>
</UL>
<A NAME="Synopsis"><H3>Synopsis</H3></A>
<PRE>#include &#60;algorithm></PRE>
<PRE>
template &#60;class ForwardIterator>
 ForwardIterator
 <B>max_element</B>(ForwardIterator first, ForwardIterator last);
template &#60;class ForwardIterator, class Compare>
 ForwardIterator
 <B>max_element</B>(ForwardIterator first, ForwardIterator last,
             Compare comp);
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>The <B><I>max_element</B></I> 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 <SAMP>comp</SAMP> defines a comparison function that can be used in place of the default <SAMP>operator&#60;</SAMP>.  This function can be used with all the datatypes provided by the standard library.</P>
<P>Algorithm <B><I>max_element</B></I> returns the first  iterator <SAMP>i</SAMP> in the range <SAMP>[first, last)</SAMP> such that for any iterator <SAMP>j</SAMP> in the same range the following corresponding conditions hold:</P>
<PRE>!(*i &#60; *j) </PRE>
<PRE></PRE><P>or</P>
<PRE>comp(*i, *j) == false. </PRE>
<A NAME="Complexity"><H3>Complexity</H3></A>
<P>Exactly <SAMP>max((last - first) - 1, 0)</SAMP> applications of the corresponding comparisons are done for <B><I>max_element</B></I>. </P>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// max_elem.cpp
//
 #include &#60;algorithm>
 #include &#60;vector>
 #include &#60;iostream.h>
 
 int main(void)
 {
   typedef vector&#60;int>::iterator iterator; 
   int d1[5] = {1,3,5,32,64};
  
   // set up vector 
   vector&#60;int>      v1(d1,d1 + 5); 
   // find the largest element in the vector
   iterator it1 = <B>max_element</B>(v1.begin(), v1.end());
   // it1 = v1.begin() + 4
 
   // find the largest element in the range from
   // the beginning of the vector to the 2nd to last
   iterator it2 = <B>max_element</B>(v1.begin(), v1.end()-1, 
                      less&#60;int>());   
   // it2 = v1.begin() + 3
 
   // find the smallest element 
   iterator it3 = min_element(v1.begin(), v1.end());  
   // it3 = v1.begin() 
 
   // find the smallest value in the range from
   // the beginning of the vector plus 1 to the end
   iterator it4 = min_element(v1.begin()+1, v1.end(), 
                      less&#60;int>());      
   // it4 = v1.begin() + 1
   cout &#60;&#60; *it1 &#60;&#60; " " &#60;&#60; *it2 &#60;&#60; " " 
        &#60;&#60; *it3 &#60;&#60; " " &#60;&#60; *it4 &#60;&#60; endl;
 
   return 0;
 }
Output :
64 32 1 3
</PRE>
<A NAME="Warning"><H3>Warning</H3></A>
<P>If your compiler does not support default template parameters then you need to always supply the <SAMP>Allocator</SAMP> template argument.  For instance you'll have to write:</P>
<PRE>vector&#60;int,allocator></PRE>
<PRE></PRE><P>instead of:</P>
<PRE>vector&#60;int></PRE>
<A NAME="See Also"><H3>See Also</H3></A>
<P><A HREF="max_6671.htm"><B><I>max</B></I></A>, <A HREF="min_9233.htm"><B><I>min</B></I></A>, <A HREF="min_9998.htm"><B><I>min_element</B></I></A></P>
<HR>
<A HREF="max_6671.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="mer_0626.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -