📄 pri_2327.htm
字号:
<HTML><TITLE>priority_queue</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>priority_queue</H2>
<HR><PRE> Container Adaptor</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>A container adapter which behaves like a priority queue. Items are popped from the queue are in order with respect to a "priority." </P>
<H3>Contents</H3>
<UL>
<A HREF="#Synopsis"><LI>Synopsis</LI></A>
<A HREF="#Description"><LI>Description</LI></A>
<A HREF="#Interface"><LI>Interface</LI></A>
<A HREF="#Constructors"><LI>Constructors</LI></A>
<A HREF="#Allocator"><LI>Allocator</LI></A>
<A HREF="#Member Functions"><LI>Member Functions</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 <queue></PRE>
<PRE>
template <class T,
class Container = vector<T>,
class Compare = less<Container::value_type>,
class Allocator = allocator>
class <B>priority_queue</B>;
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P><B><I>priority_queue</B></I> is a container adaptor which allows a container to act as a priority queue. This means that the item with the highest priority, as determined by either the default comparison operator (<SAMP>operator <</SAMP>) or the comparison <SAMP>Compare</SAMP>, is brought to the front of the queue whenever anything is pushed onto or popped off the queue. </P>
<P><B><I>priority_queue</B></I> adapts any container that provides <SAMP>front()</SAMP>, <SAMP>push_back()</SAMP> and <SAMP>pop_back()</SAMP>. In particular, <A HREF="deq_4164.htm"><B><I>deque</B></I></A>, <A HREF="lis_3222.htm"><B><I>list</B></I></A>, and <A HREF="vec_0251.htm"><B><I>vector</B></I></A> can be used.</P>
<A NAME="Interface"><H3>Interface</H3></A>
<PRE>template <class T,
class Container = vector<T>,
class Compare = less<typename Container::value_type>,
class Allocator = allocator>
class priority_queue {
public:
// typedefs
typedef typename Container::value_type value_type;
typedef typename Container::size_type size_type;
typedef Allocator allocator_type;
// Construct
explicit priority_queue (const Compare& = Compare(),
const Allocator&=Allocator());
template <class InputIterator>
priority_queue (InputIterator first,
InputIterator last,
const Compare& = Compare(),
const Allocator& = Allocator());
allocator_type get_allocator() const;
bool empty () const;
size_type size () const;
const value_type& top () const;
void push (const value_type&);
void pop();
};</PRE>
<A NAME="Constructors"><H3>Constructors</H3></A>
<PRE>explicit <B>priority_queue</B> (const Compare& x = Compare(),
const Allocator& alloc = Allocator());</PRE>
<UL><P>Default constructor. Constructs a priority queue that uses <SAMP>Container</SAMP> for its underlying implementation, <SAMP>x </SAMP>as its standard for determining priority, and the allocator <SAMP>alloc</SAMP> for all storage management.</P>
</UL>
<PRE>template <class InputIterator>
<B>priority_queue</B> (InputIterator first, InputIterator last,
const Compare& x = Compare(),
const Allocator& alloc = Allocator());</PRE>
<UL><P>Constructs a new priority queue and places into it every entity in the range <SAMP>[first, last)</SAMP>. The priority_queue will use x for determining the priority, and the allocator alloc for all storage management.</P>
</UL>
<A NAME="Allocator"><H3>Allocator</H3></A>
<PRE>allocator_type <B>get_allocator</B> () const;</PRE>
<UL><P>Returns a copy of the allocator used by self for storage management.</P>
</UL>
<A NAME="Member Functions"><H3>Member Functions</H3></A>
<PRE>bool
<B>empty </B>() const;</PRE>
<UL><P>Returns <SAMP>true</SAMP> if the priority_queue is empty, fa<SAMP>l</SAMP>se otherwise.</P>
</UL>
<PRE>void
<B>pop</B>();</PRE>
<UL><P>Removes the item with the highest priority from the queue.</P>
</UL>
<PRE>void
<B>push</B> (const value_type& x);</PRE>
<UL><P>Adds <SAMP>x</SAMP> to the queue.</P>
</UL>
<PRE>size_type
<B>size</B> () const;</PRE>
<UL><P>Returns the number of elements in the priority_queue.</P>
</UL>
<PRE>const value_type&
<B>top</B> () const;</PRE>
<UL><P>Returns a constant reference to the element in the queue with the highest priority.</P>
</UL>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// p_queue.cpp
//
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <iostream.h>
int main(void)
{
// Make a priority queue of int using a vector container
<B>priority_queue</B><int, vector<int>, less<int>, allocator> pq;
// Push a couple of values
pq.push(1);
pq.push(2);
// Pop a couple of values and examine the ends
cout << pq.top() << endl;
pq.pop();
cout << pq.top() << endl;
pq.pop();
// Make a priority queue of strings using a deque container
<B>priority_queue</B><string, deque<string>, less<string>, allocator>
pqs;
// Push on a few strings then pop them back off
int i;
for (i = 0; i < 10; i++)
{
pqs.push(string(i+1,'a'));
cout << pqs.top() << endl;
}
for (i = 0; i < 10; i++)
{
cout << pqs.top() << endl;
pqs.pop();
}
// Make a priority queue of strings using a deque
// container, and greater as the compare operation
priority_queue<string,deque<string>, greater<string>,
allocator> pgqs;
// Push on a few strings then pop them back off
for (i = 0; i < 10; i++)
{
pgqs.push(string(i+1,'a'));
cout << pgqs.top() << endl;
}
for (i = 0; i < 10; i++)
{
cout << pgqs.top() << endl;
pgqs.pop();
}
return 0;
}
Output :
2
1
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaaaaaa
aaaaaaaa
aaaaaaaaa
aaaaaaaaaa
aaaaaaaaa
aaaaaaaa
aaaaaaa
aaaaaa
aaaaa
aaaa
aaa
aa
a
a
a
a
a
a
a
a
a
a
a
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaaaaaa
aaaaaaaa
aaaaaaaaa
aaaaaaaaaa</PRE>
<A NAME="Warning"><H3>Warning</H3></A>
<P>If your compiler does not support default template parameters, you must always provide a <SAMP>Container</SAMP> template parameter, a <SAMP>Compare</SAMP> template parameter, and an <SAMP>Allocator</SAMP> template parameter when declaring an instance of <B><I>priority_queue</B></I>. For example, you would not be able to write, </P>
<PRE>priority_queue<int> var;</PRE>
<PRE></PRE><P>Instead, you would have to write,</P>
<PRE>priority_queue<int, vector<int>, </PRE>
<PRE> less<typename vector<int>::value_type, allocator> var;</PRE>
<A NAME="See Also"><H3>See Also</H3></A>
<P><A HREF="Con_2487.htm"><B><I>Containers</B></I></A>, <A HREF="que_0953.htm"><B><I>queue</B></I></A></P>
<HR>
<A HREF="pre_1548.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="ptr_4059.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -