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

📄 sor_3899.htm

📁 ARM编辑、编译软件
💻 HTM
字号:
<HTML><TITLE>sort_heap</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>sort_heap</H2>
<HR><PRE>     Algorithm</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>Converts a heap into a sorted collection.</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 RandomAccessIterator>
  void
  <B>sort_heap</B>(RandomAccessIterator first,
            RandomAccessIterator last);
template &#60;class RandomAccessIterator, class Compare>
  void
  <B>sort_heap</B>(RandomAccessIterator first,
            RandomAccessIterator last, Compare comp);
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>A heap is a particular organization of elements in a range between two random access iterators<SAMP> [a, b)</SAMP>.  Its two key properties are:</P>
<OL><LI><P><SAMP>*a</SAMP> is the largest element in the range.</P>
</LI>
<LI><P><SAMP>*a</SAMP> may be removed by <SAMP>pop_heap()</SAMP>, or a new element added by <SAMP>push_heap()</SAMP>, in O(logN) time.</P>
</LI>
</OL>
<P>These properties make heaps useful as priority queues.</P>
<P>The <B><I>sort_heap</B></I> algorithm converts a heap into a sorted collection over the range <SAMP>[first, last) </SAMP>using either the default operator (<SAMP>&#60;</SAMP>) or the comparison function supplied with the algorithm.  Note that <B><I>sort_heap</B></I> is not stable, i.e., the elements may not be in the same relative order after <B><I>sort_heap</B></I> is applied.</P>
<A NAME="Complexity"><H3>Complexity</H3></A>
<P><B><I>sort_heap</B></I> performs at most <SAMP>NlogN</SAMP> comparisons where <SAMP>N</SAMP> is equal to <SAMP>last - first</SAMP>.</P>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// heap_ops.cpp
//
 #include &#60;algorithm>
 #include &#60;vector>
 #include &#60;iostream.h>
 int main(void)
 {
   int d1[4] = {1,2,3,4};
   int d2[4] = {1,3,2,4};   
   // Set up two vectors
   vector&#60;int> v1(d1,d1 + 4), v2(d2,d2 + 4);
   // Make heaps
   make_heap(v1.begin(),v1.end());
   make_heap(v2.begin(),v2.end(),less&#60;int>());
   // v1 = (4,x,y,z)  and  v2 = (4,x,y,z)
   // Note that x, y and z represent the remaining
   // values in the container (other than 4). 
   // The definition of the heap and heap operations 
   // does not require any particular ordering
   // of these values.
   // Copy both vectors to cout
   ostream_iterator&#60;int> out(cout," ");
   copy(v1.begin(),v1.end(),out);
   cout &#60;&#60; endl;
   copy(v2.begin(),v2.end(),out);
   cout &#60;&#60; endl;
   // Now let's pop
   pop_heap(v1.begin(),v1.end());
   pop_heap(v2.begin(),v2.end(),less&#60;int>());
   // v1 = (3,x,y,4) and v2 = (3,x,y,4)
   // Copy both vectors to cout
   copy(v1.begin(),v1.end(),out);
   cout &#60;&#60; endl;
   copy(v2.begin(),v2.end(),out);
   cout &#60;&#60; endl;
   
   // And push
   push_heap(v1.begin(),v1.end());
   push_heap(v2.begin(),v2.end(),less&#60;int>());
   // v1 = (4,x,y,z) and v2 = (4,x,y,z)
   // Copy both vectors to cout
   copy(v1.begin(),v1.end(),out);
   cout &#60;&#60; endl;
   copy(v2.begin(),v2.end(),out);
   cout &#60;&#60; endl;
<B>   </B>// Now sort those heaps
   <B>sort_heap</B>(v1.begin(),v1.end());
   <B>sort_heap</B>(v2.begin(),v2.end(),less&#60;int>());
   // v1 = v2 = (1,2,3,4)
      
   // Copy both vectors to cout
   copy(v1.begin(),v1.end(),out);
   cout &#60;&#60; endl;
   copy(v2.begin(),v2.end(),out);
   cout &#60;&#60; endl;
   return 0;
 }
Output :
4 2 3 1
4 3 2 1
3 2 1 4
3 1 2 4
4 3 1 2
4 3 2 1
1 2 3 4
1 2 3 4</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 will need to write :</P>
<P><SAMP>vector&#60;int, allocator></SAMP></P>
<P>instead of :</P>
<P><SAMP>vector&#60;int></SAMP></P>
<A NAME="See Also"><H3>See Also</H3></A>
<P><A HREF="mak_0285.htm"><B><I>make_heap</B></I></A>, <A HREF="pop_9596.htm"><B><I>pop_heap</B></I></A>, <A HREF="pus_5295.htm"><B><I>push_heap</B></I></A></P>
<HR>
<A HREF="sor_1439.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="sta_4791.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>

⌨️ 快捷键说明

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