📄 priority_queue.html
字号:
<pre>
priority_queue&
operator=(const priority_queue&)
</pre>
</TD>
<TD VAlign=top>
<A href="Assignable.html">Assignable</A>
</TD>
<TD VAlign=top>
The assignment operator.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bool empty() const</tt>
</TD>
<TD VAlign=top>
<tt>priority_queue</tt>
</TD>
<TD VAlign=top>
See below.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>size_type size() const</tt>
</TD>
<TD VAlign=top>
<tt>priority_queue</tt>
</TD>
<TD VAlign=top>
See below.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>const value_type& top() const</tt>
</TD>
<TD VAlign=top>
<tt>priority_queue</tt>
</TD>
<TD VAlign=top>
See below.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>void push(const value_type&)</tt>
</TD>
<TD VAlign=top>
<tt>priority_queue</tt>
</TD>
<TD VAlign=top>
See below.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>void pop()</tt> <A href="#3">[3]</A>
</TD>
<TD VAlign=top>
<tt>priority_queue</tt>
</TD>
<TD VAlign=top>
See below.
</TD>
</tr>
</table>
<h3>New members</h3>
These members are not defined in the
<A href="Assignable.html">Assignable</A> and <A href="DefaultConstructible.html" tppabs="http://www.sgi.com/Technology/STL/DefaultConstructible.shtml">Default Constructible</A>
requirements, but are specific to
<tt>priority_queue</tt>.
<Table border>
<TR>
<TH>
Member
</TH>
<TH>
Description
</TH>
</TR>
<TR>
<TD VAlign=top>
<tt>value_type</tt>
</TD>
<TD VAlign=top>
The type of object stored in the <tt>priority_queue</tt>. This is the same as
<tt>T</tt> and <tt>Sequence::value_type</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>size_type</tt>
</TD>
<TD VAlign=top>
An unsigned integral type. This is the same as <tt>Sequence::size_type</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>priority_queue(const Compare& comp)</tt>
</TD>
<TD VAlign=top>
The constructor. Creates an empty <tt>priority_queue</tt>, using <tt>comp</tt>
as the comparison function. The default constructor uses
<tt>Compare()</tt> as the comparison function.
</TD>
</TR>
<TR>
<TD VAlign=top>
<pre>
priority_queue(const value_type* first,
const value_type* last)
</pre>
</TD>
<TD VAlign=top>
The constructor. Creates a <tt>priority_queue</tt> initialized to contain
the elements in the range <tt>[first, last)</tt>, and using <tt>Compare()</tt> as
the comparison function.
</TD>
</TR>
<TR>
<TD VAlign=top>
<pre>
priority_queue(const value_type* first,
const value_type* last,
const Compare& comp)
</pre>
</TD>
<TD VAlign=top>
The constructor. Creates a <tt>priority_queue</tt> initialized to contain
the elements in the range <tt>[first, last)</tt>, and using <tt>comp</tt> as
the comparison function.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bool empty() const</tt>
</TD>
<TD VAlign=top>
Returns <tt>true</tt> if the <tt>priority_queue</tt> contains no elements, and <tt>false</tt>
otherwise. <tt>S.empty()</tt> is equivalent to <tt>S.size() == 0</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>size_type size() const</tt>
</TD>
<TD VAlign=top>
Returns the number of elements contained in the <tt>priority_queue</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>const value_type& top() const</tt>
</TD>
<TD VAlign=top>
Returns a const reference to the element at the top of the priority_queue.
The element at the top is guaranteed to be the largest element in
the priority queue, as determined by the comparison function <tt>Compare</tt>.
That is, for every other element <tt>x</tt> in the <tt>priority_queue</tt>,
<tt>Compare(Q.top(), x)</tt> is <tt>false</tt>.
Precondition: <tt>empty()</tt> is <tt>false</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>void push(const value_type& x)</tt>
</TD>
<TD VAlign=top>
Inserts <tt>x</tt> into the priority_queue. Postcondition: <tt>size()</tt> will
be incremented by <tt>1</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>void pop()</tt>
</TD>
<TD VAlign=top>
Removes the element at the top of the priority_queue, that is, the
largest element in the priority_queue. <A href="#3">[3]</A> Precondition:
<tt>empty()</tt> is <tt>false</tt>. Postcondition: <tt>size()</tt> will be decremented
by <tt>1</tt>.
</TD>
</tr>
</table>
<h3>Notes</h3>
<P><A name="1">[1]</A>
Priority queues are a standard concept, and can be implemented in
many different ways; this implementation uses heaps. Priority queues
are discussed in all algorithm books; see, for example, section 5.2.3
of Knuth. (D. E. Knuth, <i>The Art of Computer
Programming. Volume 3: Sorting and Searching</i>.
Addison-Wesley, 1975.)
<P><A name="2">[2]</A>
This restriction is the only reason for <tt>priority_queue</tt> to exist
at all. If iteration through elements is important, you can either
use a <tt><A href="Vector.html">vector</A></tt> that is maintained in sorted order, or a <tt><A href="set.html" tppabs="http://www.sgi.com/Technology/STL/set.shtml">set</A></tt>,
or a <tt><A href="Vector.html">vector</A></tt> that is maintained as a heap using <tt><A href="make_heap.html" tppabs="http://www.sgi.com/Technology/STL/make_heap.shtml">make_heap</A></tt>,
<tt><A href="push_heap.html">push_heap</A></tt>, and <tt><A href="pop_heap.html" tppabs="http://www.sgi.com/Technology/STL/pop_heap.shtml">pop_heap</A></tt>. <tt>Priority_queue</tt> is, in fact,
implemented as a <A href="RandomAccessContainer.html">random access container</A> that is maintained as
a heap. The only reason to use the container
adaptor <tt>priority_queue</tt>, instead of performing the heap operations
manually, is to make it clear that you are never performing
any operations that might violate the heap invariant.
<P><A name="3">[3]</A>
One might wonder why <tt>pop()</tt> returns <tt>void</tt>, instead of
<tt>value_type</tt>. That is, why must one use <tt>top()</tt> and <tt>pop()</tt> to
examine and remove the element at the top of the <tt>priority_queue</tt>, instead of
combining the two in a single member function? In fact, there is a
good reason for this design. If <tt>pop()</tt> returned the top element, it
would have to return by value rather than by reference: return by
reference would create a dangling pointer. Return by value, however,
is inefficient: it involves at least one redundant copy constructor
call. Since it is impossible for <tt>pop()</tt> to return a value in such a
way as to be both efficient and correct, it is more sensible for it to
return no value at all and to require clients to use <tt>top()</tt> to
inspect the value at the top of the <tt>priority_queue</tt>.
<h3>See also</h3>
<tt><A href="stack.html">stack</A></tt>, <tt><A href="queue.html" tppabs="http://www.sgi.com/Technology/STL/queue.shtml">queue</A></tt>, <tt><A href="set.html" tppabs="http://www.sgi.com/Technology/STL/set.shtml">set</A></tt>,
<tt><A href="make_heap.html">make_heap</A></tt>, <tt><A href="push_heap.html" tppabs="http://www.sgi.com/Technology/STL/push_heap.shtml">push_heap</A></tt>, <tt><A href="pop_heap.html" tppabs="http://www.sgi.com/Technology/STL/pop_heap.shtml">pop_heap</A></tt>, <tt><A href="is_heap.html" tppabs="http://www.sgi.com/Technology/STL/is_heap.shtml">is_heap</A></tt>,
<tt><A href="sort.html">sort</A></tt>, <A href="is_sorted.html" tppabs="http://www.sgi.com/Technology/STL/is_sorted.shtml">is_sorted</A>,
<A href="Container.html">Container</A>, <A href="SortedAssociativeContainer.html" tppabs="http://www.sgi.com/Technology/STL/SortedAssociativeContainer.shtml">Sorted Associative Container</A>, <A href="Sequence.html" tppabs="http://www.sgi.com/Technology/STL/Sequence.shtml">Sequence</A>
<HR SIZE="6"> <FONT SIZE="-2"> Copyright © 1996 Silicon Graphics, Inc.
<HR>
<TABLE BORDER=0 WIDTH="100%" >
<TR>
<TD WIDTH="33%"><FONT SIZE=-1><A HREF="index.html" >
STL</A></FONT></TD>
<TD WIDTH="33%">
<CENTER><FONT SIZE=-2>© Copyright 1997-1998 CodeGuru</FONT> </CENTER>
</TD>
<TD WIDTH="34%">
<DIV ALIGN=right><FONT SIZE=-1>Contact : <A HREF="mailto:webmaster@codeguru.com">webmaster@codeguru.com</A> </FONT></DIV>
</TD>
</TR>
</TABLE>
<SCRIPT LANGUAGE="JavaScript" ><!--
var adurl = "/cgi-bin/doubleclick.cgi?";
if( self.adcategory )
adurl += adcategory;
else
adurl += "mfc";
if( self.parent.norefreshad )
parent.norefreshad = false;
else if( validframes )
parent.frames['ad'].location = adurl;
if( !validframes && nfrm == -1)
{
var dclkPage = "www.codeguru.com/";
if( self.adcategory )
dclkPage += adcategory;
else
dclkPage += "mfc";
// var random = Math.random();
document.write('<nolayer><center>');
document.write('<iframe src="http://ad.doubleclick.net/adi/' + dclkPage + ';ord='
+ random + '" width=470 height=62 marginwidth=0 marginheight=0 hspace=0 vspace=0 '
+ 'frameborder=0 scrolling=no bordercolor="#000000">');
document.write('<a href="http://ad.doubleclick.net/jump/' + dclkPage + ';ord='
+ random + '">');
document.write('<img src="http://ad.doubleclick.net/ad/' + dclkPage + ';ord='
+ random + '" height=60 width=468>' + '</a>');
document.write('</iframe>');
document.write('</center></nolayer>');
document.write('<layer src="http://ad.doubleclick.net/adl/' + dclkPage +
';ord=' + random + '"></layer>');
document.write('<ilayer visibility=hide width=468 height=83></ilayer>');
}
// -->
</SCRIPT>
<!-- SCRIPT LANGUAGE="JavaScript" SRC="/global/fscript.js">
//
</SCRIPT -->
<noscript>
<p align="center">
<a href="http://ad.doubleclick.net/jump/www.codeguru.com/cpp;ord=NupacNFCY34AAHczWRI">
<img src="http://ad.doubleclick.net/ad/www.codeguru.com/cpp;ord=NupacNFCY34AAHczWRI"></a>
</p>
</noscript>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -