.\" ident @(#)priority_queue.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH priority_queue 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2priority_queue\fP \ - A container adapter that behaves like a priority queue. Items popped from the queue are in order with respect to a "priority." .SH SYNOPSIS .RE .RS 0 #include .br template , .br class Compare = less > .RE .RS 0 class priority_queue; .SH DESCRIPTION priority_queue 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 the default comparison operator (\f2operator <\fP) or the comparison \f2Compare\fP, is brought to the front of the queue whenever anything is pushed onto or popped off the queue. priority_queue adapts any container that gives \f2front()\fP, \f2push_back()\fP, and \f2pop_back()\fP. In particular, deque and vector can be used. .SH INTERFACE .br template , .RE .RS 9 class Compare = less > .RE .RS 0 class priority_queue { .br public: .br .br // typedefs .RE .RS 2 typedef typename Container::value_type value_type; .br typedef typename Container::size_type size_type; .br typedef Container container_type; .RE .RS 0 .br // Construct .RE .RS 2 explicit priority_queue (const Compare& = Compare(), .RE .RS 27 const Container& = Container()); .RE .RS 2 template .RE .RS 4 priority_queue (InputIterator first, .RE .RS 20 InputIterator last, .br const Compare& = Compare(), .br const Container& = Container()); .RE .RS 2 bool empty () const; .br size_type size () const; .br const value_type& top () const; .br void push (const value_type&); .br void pop(); .RE .RS 0 }; .SH CONSTRUCTORS .br explicit priority_queue (const Compare& x = Compare(), .RE .RS 24 const Container& = Container()); .RE .RS 3 Constructs a priority queue that uses \f2Container\fP for its underlying implementation, \f2x \fPas its standard for determining priority, and the allocator Allocator\f2()\fP for all storage management. .RE .RE .RS 0 template .br priority_queue (InputIterator first, InputIterator last, .RE .RS 15 const Compare& x = Compare(), .br const allocator_type& alloc = .br allocator_type()); .RE .RS 3 Constructs a new priority queue and places into it every entity in the range \f2[first, last)\fP. The priority_queue uses \f2x\fP for determining the priority, and the allocator \f2alloc\fP for all storage management. .RE .SH MEMBER FUNCTIONS .RE .RS 0 bool .br empty () const; .RE .RS 3 Returns \f2true\fP if the priority_queue is empty, \f2false\fP otherwise. .RE .br void .br pop(); .RE .RS 3 Removes the item with the highest priority from the queue. .RE .br void .br push (const value_type& x); .RE .RS 3 Adds \f2x\fP to the queue. .RE .br size_type .br size () const; .RE .RS 3 Returns the number of elements in the priority_queue. .RE .br const value_type& .br top () const; .RE .RS 3 Returns a constant reference to the element in the queue with the highest priority. .RE .SH EXAMPLE .br // .br // p_queue.cpp .br // .RE .RS 1 #include .br #include .br #include .br #include .br #include .RE .RS 0 using namespace std; .br .br int main(void) .RE .RS 1 { .RE .RS 3 // Make a priority queue of int using a vector container .br priority_queue, less > pq; .RE .RS 1 .RE .RS 3 // Push a couple of values .RE .RS 2 pq.push(1); .br pq.push(2); .RE .RS 0 .RE .RS 3 // Pop a couple of values and examine the ends .RE .RS 2 cout << pq.top() << endl; .br pq.pop(); .br cout << pq.top() << endl; .br pq.pop(); .RE .RS 0 .RE .RS 3 // Make a priority queue of strings using .br // a deque container .br priority_queue, less > .RE .RS 5 pqs; .RE .RS 0 .RE .RS 3 // Push on a few strings then pop them back off .RE .RS 2 int i; .br for (i = 0; i < 10; i++) .RE .RS 3 { .RE .RS 4 pqs.push(string(i+1,'a')); .br cout << pqs.top() << endl; .RE .RS 3 } .RE .RS 2 for (i = 0; i < 10; i++) .RE .RS 3 { .RE .RS 4 cout << pqs.top() << endl; .br pqs.pop(); .RE .RS 3 } .RE .RS 0 .RE .RS 3 // Make a priority queue of strings using a deque .br // container, and greater as the compare operation .br priority_queue, greater > .RE .RS 5 pgqs; .RE .RS 0 .RE .RS 3 // Push on a few strings then pop them back off .RE .RS 2 for (i = 0; i < 10; i++) .RE .RS 3 { .RE .RS 4 pgqs.push(string(i+1,'a')); .br cout << pgqs.top() << endl; .RE .RS 3 } .RE .RS 0 .RE .RS 2 for (i = 0; i < 10; i++) .RE .RS 3 { .RE .RS 4 cout << pgqs.top() << endl; .br pgqs.pop(); .RE .RS 3 } .RE .RS 0 .RE .RS 2 return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br 2 .br 1 .br a .br aa .br aaa .br aaaa .br aaaaa .br aaaaaa .br aaaaaaa .br aaaaaaaa .br aaaaaaaaa .br aaaaaaaaaa .br aaaaaaaaa .br aaaaaaaa .br aaaaaaa .br aaaaaa .br aaaaa .br aaaa .br aaa .br aa .br a .br a .br a .br a .br a .br a .br a .br a .br a .br a .br a .br a .br aa .br aaa .br aaaa .br aaaaa .br aaaaaa .br aaaaaaa .br aaaaaaaa .br aaaaaaaaa .br aaaaaaaaaa .SH WARNINGS If your compiler does not support default template parameters, you must always include a \f2Container\fP template parameter and a \f2Compare\fP template parameter when declaring an instance of priority_queue. For example, you would not be able to write: .br priority_queue var; Instead, you would have to write: .br \f2priority_queue, \fP .br \f2 less::value_type> > var;\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP. .SH SEE ALSO Containers, queue