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

📄 pri_2327.htm

📁 ARM编辑、编译软件
💻 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>&copy;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 &#60;queue></PRE>
<PRE>
template &#60;class T,
          class Container = vector&#60;T>,
          class Compare = less&#60;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 &#60;</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 &#60;class T,
          class Container = vector&#60;T>,
          class Compare = less&#60;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&#38; = Compare(),
                            const Allocator&#38;=Allocator());
   template &#60;class InputIterator>
     priority_queue (InputIterator first,
                     InputIterator last,
                     const Compare&#38; = Compare(), 
                     const Allocator&#38; = Allocator());
   allocator_type get_allocator() const;
   bool empty () const;
   size_type size () const;
   const value_type&#38; top () const;
   void push (const value_type&#38;);
   void pop();
};</PRE>
<A NAME="Constructors"><H3>Constructors</H3></A>
<PRE>explicit <B>priority_queue</B> (const Compare&#38; x = Compare(),
                         const Allocator&#38; 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 &#60;class InputIterator>
<B>priority_queue</B> (InputIterator first, InputIterator last,
                const Compare&#38; x = Compare(),
                const Allocator&#38; 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&#38; 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&#38; 
<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 &#60;queue>
 #include &#60;deque>
 #include &#60;vector>
 #include &#60;string>
 #include &#60;iostream.h>
 int main(void)
 {
   // Make a priority queue of int using a vector container
   <B>priority_queue</B>&#60;int, vector&#60;int>, less&#60;int>, allocator> pq;
 
   // Push a couple of values
   pq.push(1);
   pq.push(2);
   // Pop a couple of values and examine the ends
   cout &#60;&#60; pq.top() &#60;&#60; endl;
   pq.pop();
   cout &#60;&#60; pq.top() &#60;&#60; endl;
   pq.pop();
   // Make a priority queue of strings using a deque container
   <B>priority_queue</B>&#60;string, deque&#60;string>, less&#60;string>, allocator>
      pqs;
   // Push on a few strings then pop them back off
   int i;
   for (i = 0; i &#60; 10; i++)
   {
     pqs.push(string(i+1,'a'));
     cout &#60;&#60; pqs.top() &#60;&#60; endl;
   }
   for (i = 0; i &#60; 10; i++)
   {
     cout &#60;&#60; pqs.top() &#60;&#60; endl;
     pqs.pop();
   }
   // Make a priority queue of strings using a deque    
   // container, and greater as the compare operation
   priority_queue&#60;string,deque&#60;string>, greater&#60;string>,    
     allocator> pgqs;
   // Push on a few strings then pop them back off
   for (i = 0; i &#60; 10; i++)
   {
     pgqs.push(string(i+1,'a'));
     cout &#60;&#60; pgqs.top() &#60;&#60; endl;
   }
   for (i = 0; i &#60; 10; i++)
   {
     cout &#60;&#60; pgqs.top() &#60;&#60; 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&#60;int> var;</PRE>
<PRE></PRE><P>Instead, you would have to write,</P>
<PRE>priority_queue&#60;int, vector&#60;int>, </PRE>
<PRE>  less&#60;typename vector&#60;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 + -