📄 pop_9596.htm
字号:
<HTML><TITLE>pop_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>©Copyright 1996 Rogue Wave Software</P>
<H2>pop_heap</H2>
<HR><PRE> Algorithms</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>Moves the largest element off the heap.</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>template <class RandomAccessIterator></PRE>
<PRE> void
<B>pop_heap</B>(RandomAccessIterator first,
RandomAccessIterator last);
template <class RandomAccessIterator, class Compare>
void
<B>pop_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 the <B><I>pop_heap</B></I> algorithm or a new element added by the <A HREF="pus_5295.htm"><B><I>push_heap</B></I></A> algorithm, in <SAMP>O(logN)</SAMP> time.</P>
</LI>
</OL>
<P>These properties make heaps useful as priority queues.</P>
<P>The <B><I>pop_heap</B></I> algorithm uses the less than (<SAMP><</SAMP>) operator as the default comparison. An alternate comparison operator can be specified.</P>
<P>The <B><I>pop_heap</B></I> algorithm can be used as part of an operation to remove the largest element from a heap. It assumes that the range <SAMP>[first, last)</SAMP> is a valid heap (i.e., that <SAMP>first</SAMP> is the largest element in the heap or the first element based on the alternate comparison operator). It then swaps the value in the location <SAMP>first</SAMP> with the value in the location<SAMP> last - 1</SAMP> and makes <SAMP>[first, last -1)</SAMP>back into a heap. You can then access the element in <SAMP>last</SAMP> using the vector or deque <SAMP>back()</SAMP> member function, or remove the element using the <SAMP>pop_back</SAMP> member function. Note that <B><I>pop_heap</B></I> does not actually remove the element from the data structure, you must use another function to do that. </P>
<A NAME="Complexity"><H3>Complexity</H3></A>
<P><B><I>pop_heap</B></I> performs at most <SAMP>2 * log(last - first)</SAMP> comparisons.</P>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// heap_ops.cpp
//
#include <algorithm>
#include <vector>
#include <iostream.h>
int main(void)
{
int d1[4] = {1,2,3,4};
int d2[4] = {1,3,2,4};
// Set up two vectors
vector<int> v1(d1,d1 + 4), v2(d2,d2 + 4);
// Make heaps
make_heap(v1.begin(),v1.end());
make_heap(v2.begin(),v2.end(),less<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<int> out(cout," ");
copy(v1.begin(),v1.end(),out);
cout << endl;
copy(v2.begin(),v2.end(),out);
cout << endl;
// Now let's pop
<B> pop_heap</B>(v1.begin(),v1.end());
<B>pop_heap</B>(v2.begin(),v2.end(),less<int>());
// v1 = (3,x,y,4) and v2 = (3,x,y,4)
// Copy both vectors to cout
copy(v1.begin(),v1.end(),out);
cout << endl;
copy(v2.begin(),v2.end(),out);
cout << endl;
// And push
push_heap(v1.begin(),v1.end());
push_heap(v2.begin(),v2.end(),less<int>());
// v1 = (4,x,y,z) and v2 = (4,x,y,z)
// Copy both vectors to cout
copy(v1.begin(),v1.end(),out);
cout << endl;
copy(v2.begin(),v2.end(),out);
cout << endl;
// Now sort those heaps
sort_heap(v1.begin(),v1.end());
sort_heap(v2.begin(),v2.end(),less<int>());
// v1 = v2 = (1,2,3,4)
// Copy both vectors to cout
copy(v1.begin(),v1.end(),out);
cout << endl;
copy(v2.begin(),v2.end(),out);
cout << 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, you need to always supply the <SAMP>Allocator</SAMP> template argument. For instance, you need to write :</P>
<P><SAMP>vector<int, allocator></SAMP></P>
<P>instead of :</P>
<P><SAMP>vector<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="pus_5295.htm"><B><I>push_heap</B></I></A>, <A HREF="sor_3899.htm"><B><I>sort_heap</B></I></A></P>
<HR>
<A HREF="poi_7375.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="Pre_1511.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -