blob: 26afa1e69d0a9f8dcbd60c5b0830f75a8978f51e [file] [log] [blame]
<HTML>
<HEAD>
<TITLE>sort_heap()</TITLE>
<LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Apache stdcxx Stylesheet"></HEAD>
<BODY BGCOLOR=#FFFFFF>
<A HREF="sort.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="sstream-h.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Apache C++ Standard Library Reference Guide</B></DIV>
<H2>sort_heap()</H2>
<P><B>Library:</B>&nbsp;&nbsp;<A HREF="2-9.html">Algorithms</A></P>
<PRE><HR><B><I>Function</I></B><HR></PRE>
<UL>
<LI><A HREF="#sec1">Local Index</A></LI>
<LI><A HREF="#sec2">Summary</A></LI>
<LI><A HREF="#sec3">Synopsis</A></LI>
<LI><A HREF="#sec4">Description</A></LI>
<LI><A HREF="#sec5">Complexity</A></LI>
<LI><A HREF="#sec6">Example</A></LI>
<LI><A HREF="#sec7">See Also</A></LI>
<LI><A HREF="#sec8">Standards Conformance</A></LI>
</UL>
<A NAME="sec1"><H3>Local Index</H3></A>
No Entries
<A NAME="sec2"><H3>Summary</H3></A>
<P>An algorithm that converts a heap into a sorted collection</P>
<A NAME="sec3"><H3>Synopsis</H3></A>
<PRE>#include &lt;algorithm&gt;
namespace std {
template &lt;class RandomAccessIterator&gt;
void
sort_heap(RandomAccessIterator start,
RandomAccessIterator finish);
template &lt;class RandomAccessIterator, class Compare&gt;
void
sort_heap(RandomAccessIterator start,
RandomAccessIterator finish, Compare comp);
}
</PRE>
<A NAME="sec4"><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>
<UL>
<LI><P CLASS="LIST"><SAMP>*a</SAMP> is the largest element in the range.</P></LI>
<LI><P CLASS="LIST"><SAMP>*a</SAMP> may be removed by <SAMP><A HREF="pop-heap.html">pop_heap()</A></SAMP> or a new element may be added by <SAMP><A HREF="push-heap.html">push_heap()</A></SAMP>, in <SAMP>O(logN)</SAMP> time.</P></LI>
</UL>
<P>These properties make heaps useful as priority queues.</P>
<P>The <SAMP>sort_heap()</SAMP> algorithm converts a heap into a sorted collection over the range <SAMP>[start, finish) </SAMP>using either <SAMP>operator&lt;()</SAMP> or the function object <SAMP>comp</SAMP>. Note that <SAMP>sort_heap()</SAMP> is not stable; equivalent elements are not guaranteed to remain in the same relative order after <SAMP>sort_heap()</SAMP> is applied.</P>
<A NAME="sec5"><H3>Complexity</H3></A>
<P><SAMP>sort_heap()</SAMP> performs approximately <SAMP>N * log(N)</SAMP> comparisons, where <SAMP>N</SAMP> is equal to <SAMP>finish - start</SAMP>.</P>
<A NAME="sec6"><H3>Example</H3></A>
<UL><PRE>//
// heap_ops.cpp
//
#include &lt;algorithm&gt; // for copy, make_heap, pop_heap,
// and push_heap
#include &lt;functional&gt; // for less
#include &lt;iostream&gt; // for cout
#include &lt;iterator&gt; // for ostream_iterator
#include &lt;vector&gt; // for vector
template &lt;class charT, class Traits, class T, class Allocator&gt;
void print_vector (std::basic_ostream&lt;charT, Traits&gt; &amp;strm,
const std::vector&lt;T, Allocator&gt; &amp;v)
{
std::copy (v.begin (), v.end (),
std::ostream_iterator&lt;T, charT, Traits&gt;
(strm, " "));
strm &lt;&lt; std::endl;
}
int main ()
{
typedef std::vector&lt;int, std::allocator&lt;int&gt; &gt; Vector;
const Vector::value_type d1[] = { 1, 2, 3, 4 };
const Vector::value_type d2[] = { 1, 3, 2, 4 };
// Set up two vectors.
Vector v1 (d1 + 0, d1 + sizeof d1 / sizeof *d1);
Vector v2 (d2 + 0, d2 + sizeof d2 / sizeof *d2);
// Make heaps.
std::make_heap (v1.begin (), v1.end ());
std::make_heap (v2.begin (), v2.end (), std::less&lt;int&gt;());
// 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.
print_vector (std::cout, v1);
print_vector (std::cout, v2);
// Now let's pop.
std::pop_heap (v1.begin (), v1.end ());
std::pop_heap (v2.begin (), v2.end (), std::less&lt;int&gt;());
print_vector (std::cout, v1);
print_vector (std::cout, v2);
// And push.
std::push_heap (v1.begin (), v1.end ());
std::push_heap (v2.begin (), v2.end (), std::less&lt;int&gt;());
print_vector (std::cout, v1);
print_vector (std::cout, v2);
// Now sort those heaps.
std::sort_heap (v1.begin (), v1.end ());
std::sort_heap (v2.begin (), v2.end (), std::less&lt;int&gt;());
print_vector (std::cout, v1);
print_vector (std::cout, v2);
return 0;
}
Program Output:
</PRE></UL>
<UL><PRE>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></UL>
<A NAME="sec7"><H3>See Also</H3></A>
<P><SAMP><A HREF="make-heap.html">make_heap()</A></SAMP>, <SAMP><A HREF="pop-heap.html">pop_heap()</A></SAMP>, <SAMP><A HREF="push-heap.html">push_heap()</A></SAMP></P>
<A NAME="sec8"><H3>Standards Conformance</H3></A>
<P><I>ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 25.3.6.4</I></P>
<BR>
<HR>
<A HREF="sort.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="sstream-h.html"><IMG SRC="images/bnext.gif" WIDTH=20 HEIGHT=21 ALT="Next file" BORDER=O></A>
<!-- Google Analytics tracking code -->
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-1775151-1";
urchinTracker();
</script>
<!-- end of Google Analytics tracking code -->
</BODY>
</HTML>