| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one or more |
| contributor license agreements. See the NOTICE file distributed |
| with this work for additional information regarding copyright |
| ownership. The ASF licenses this file to you under the Apache |
| License, Version 2.0 (the License); you may not use this file |
| except in compliance with the License. You may obtain a copy of |
| the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| implied. See the License for the specific language governing |
| permissions and limitations under the License. |
| |
| Copyright 1999-2007 Rogue Wave Software, Inc. |
| --> |
| |
| <HTML> |
| <HEAD> |
| <TITLE>priority_queue</TITLE> |
| <LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Apache stdcxx Stylesheet"></HEAD> |
| <BODY BGCOLOR=#FFFFFF> |
| <A HREF="prev-permutation.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="ptr-fun.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>priority_queue</H2> |
| <P><B>Library:</B> <A HREF="2-7.html">Containers</A></P> |
| |
| <PRE><HR><B><I>Does not inherit</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">Interface</A></LI> |
| <LI><A HREF="#sec6">Constructors</A></LI> |
| <LI><A HREF="#sec7">Member Functions</A></LI> |
| <LI><A HREF="#sec8">Example</A></LI> |
| <LI><A HREF="#sec9">Warnings</A></LI> |
| <LI><A HREF="#sec10">See Also</A></LI> |
| <LI><A HREF="#sec11">Standards Conformance</A></LI> |
| </UL> |
| <A NAME="sec1"><H3>Local Index</H3></A> |
| <H4>Members</H4> |
| <UL><TABLE CELLPADDING=3> |
| <TR><TD VALIGN=top> |
| <A HREF="#idx1106">empty()</A><BR> |
| <A HREF="#idx1107">pop()</A><BR> |
| </TD> |
| <TD VALIGN=top><A HREF="#idx1104">priority_queue()</A><BR> |
| <A HREF="#idx1108">push()</A><BR> |
| </TD> |
| <TD VALIGN=top><A HREF="#idx1109">size()</A><BR> |
| <A HREF="#idx1110">top()</A><BR> |
| </TD> |
| <TD VALIGN=top></TD></TR> |
| </TABLE></UL> |
| |
| <A NAME="sec2"><H3>Summary</H3></A> |
| <P>A container adapter that behaves like a priority queue. Items popped from the queue are in order with respect to a priority.</P> |
| <A NAME="sec3"><H3>Synopsis</H3></A> |
| |
| <PRE>#include <queue> |
| |
| namespace std { |
| template <class T, |
| class Container = vector<T>, |
| class Compare = less<Container::value_type> > |
| class priority_queue; |
| } |
| </PRE> |
| <A NAME="sec4"><H3>Description</H3></A> |
| <P><B><I>priority_queue</I></B> is a container adaptor that allows a container to act as a priority queue. This means that the item with the highest priority, as determined by either <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</I></B> adapts any container that supports <SAMP>front()</SAMP>, <SAMP>push_back()</SAMP>, <SAMP>pop_back()</SAMP>, and has a random access iterator. In particular, <B><I><A HREF="deque.html">deque</A></I></B> and <B><I><A HREF="vector.html">vector</A></I></B> can be used. To instantiate a <B><I>priority_queue</I></B>, a comparison function object must be supplied.</P> |
| <A NAME="sec5"><H3>Interface</H3></A> |
| |
| <UL><PRE>namespace std { |
| |
| template <class T, class Container = vector<T>, |
| class Compare = less<typename |
| Container::value_type> > |
| class priority_queue { |
| |
| public: |
| |
| // typedefs |
| |
| typedef typename Container::value_type value_type; |
| typedef typename Container::size_type size_type; |
| typedef Container container_type; |
| |
| // Construct |
| |
| explicit priority_queue(const Compare& = Compare(), |
| const Container& = Container()); |
| template <class InputIterator> |
| priority_queue(InputIterator start, |
| InputIterator finish, |
| const Compare& = Compare(), |
| const Container& = Container()); |
| |
| // Accessors |
| |
| bool empty() const; |
| size_type size ) const; |
| const value_type& top() const; |
| void push(const value_type&); |
| void pop(); |
| }; |
| } |
| </PRE></UL> |
| <A NAME="sec6"><H3>Constructors</H3></A> |
| |
| <A NAME="idx1104"></A><PRE>explicit <B>priority_queue</B>(const Compare& x = Compare(), |
| const Container& = Container());</PRE> |
| <UL> |
| <P>Constructs a priority queue that uses <SAMP>Container</SAMP> for its underlying implementation, <SAMP>x </SAMP>as its standard for determining priority.</P> |
| </UL> |
| |
| |
| <A NAME="idx1105"></A><PRE>template <class InputIterator> |
| <B>priority_queue</B>(InputIterator start, InputIterator finish, |
| const Compare& x = Compare(), |
| const allocator_type& alloc = |
| allocator_type());</PRE> |
| <UL> |
| <P>Constructs a new priority queue and places into it every entity in the range <SAMP>[start, finish)</SAMP>. The priority queue uses <SAMP>x</SAMP> for determining the priority, and the allocator <SAMP>alloc</SAMP> for all storage management.</P> |
| </UL> |
| |
| <A NAME="sec7"><H3>Member Functions</H3></A> |
| |
| <A NAME="idx1106"></A><PRE>bool |
| <B>empty</B>() const;</PRE> |
| <UL> |
| <P>Returns <SAMP>true</SAMP> if the priority queue is empty, <SAMP>false</SAMP> otherwise.</P> |
| </UL> |
| |
| |
| <A NAME="idx1107"></A><PRE>void |
| <B>pop</B>();</PRE> |
| <UL> |
| <P>Removes the item with the highest priority from the queue.</P> |
| </UL> |
| |
| |
| <A NAME="idx1108"></A><PRE>void |
| <B>push</B>(const value_type& x);</PRE> |
| <UL> |
| <P>Adds <SAMP>x</SAMP> to the queue.</P> |
| </UL> |
| |
| |
| <A NAME="idx1109"></A><PRE>size_type |
| <B>size</B>() const;</PRE> |
| <UL> |
| <P>Returns the number of elements in the priority queue.</P> |
| </UL> |
| |
| |
| <A NAME="idx1110"></A><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="sec8"><H3>Example</H3></A> |
| |
| <UL><PRE>// |
| // p_queue.cpp |
| // |
| |
| #include <deque> // for deque |
| #include <iostream> // for cout, endl |
| #include <queue> // for priority_queue |
| #include <string> // for string |
| #include <vector> // for vector |
| |
| |
| |
| int main () |
| { |
| // Make a priority queue of int using a vector container. |
| std::priority_queue<int, |
| std::vector<int, |
| std::allocator<int> >, |
| std::less<int> > pq; |
| // Push a couple of values. |
| pq.push (1); |
| pq.push (2); |
| |
| // Pop a couple of values and examine the ends. |
| std::cout << pq.top () << std::endl; |
| pq.pop (); |
| std::cout << pq.top () << std::endl; |
| pq.pop (); |
| |
| // Make a priority queue of strings. |
| std::priority_queue<std::string, |
| std::deque<std::string, |
| std::allocator<std::string> >, |
| std::less<std::string> > pqs; |
| |
| // Push on a few strings then pop them back off. |
| for (int i = 0; i < 10; i++) { |
| pqs.push (std::string (i + 1, 'a')); |
| std::cout << pqs.top () << std::endl; |
| } |
| |
| for (int j = 0; j < 10; j++) { |
| std::cout << pqs.top () << std::endl; |
| pqs.pop (); |
| } |
| |
| // Make a priority queue of strings using greater. |
| std::priority_queue<std::string, |
| std::deque<std::string, |
| std::allocator<std::string> >, |
| std::greater<std::string> > pgqs; |
| |
| // Push on a few strings then pop them back off. |
| for (int k = 0; k < 10; k++) { |
| pgqs.push (std::string (k + 1, 'a')); |
| std::cout << pgqs.top () << std::endl; |
| } |
| |
| for (int m = 0; m < 10; m++) { |
| std::cout << pgqs.top () << std::endl; |
| pgqs.pop (); |
| } |
| |
| return 0; |
| } |
| |
| |
| Program Output: |
| </PRE></UL> |
| <UL><PRE>2 |
| 1 |
| a |
| aa |
| aaa |
| aaaa |
| aaaaa |
| aaaaaa |
| aaaaaaa |
| aaaaaaaa |
| aaaaaaaaa |
| aaaaaaaaaa |
| 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></UL> |
| <A NAME="sec9"><H3>Warnings</H3></A> |
| <P>If your compiler does not support default template parameters, you must always include a <SAMP>Container</SAMP> template parameter and a <SAMP>Compare</SAMP> template parameter when declaring an instance of <B><I>priority_queue</I></B>. For example, you must write:</P> |
| |
| <UL><PRE>priority_queue<int, vector<int>, |
| less<typename vector<int>::value_type> > var; |
| </PRE></UL> |
| <P>instead of:</P> |
| |
| <UL><PRE>priority_queue<int> var; |
| </PRE></UL> |
| <A NAME="sec10"><H3>See Also</H3></A> |
| <P><A HREF="containers.html">Containers</A>, <B><I><A HREF="queue.html">queue</A></I></B></P> |
| <A NAME="sec11"><H3>Standards Conformance</H3></A> |
| <P><I>ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 23.2.3.2</I></P> |
| |
| <BR> |
| <HR> |
| <A HREF="prev-permutation.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="ptr-fun.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> |