C++ Priority Queues

C++ Reference

empty
Syntax:
  #include <queue>
  bool empty() const;

The empty() function returns true if the priority queue has no elements, false otherwise.

For example, the following code uses empty() as the stopping condition on a (C/C++ Keywords) while loop to clear a priority queue and display its contents in reverse order:

 vector<int> v;
 for( int i = 0; i < 5; i++ ) {
   v.push_back(i);
 }
 while( !v.empty() ) {
   cout << v.back() << endl;
   v.pop_back();
 }              
Related topics:

pop
Syntax:
  #include <queue>
  void pop();

The function pop() removes the top element of the priority queue and discards it.

Related topics:

Priority queue constructors
Syntax:
  #include <queue>
  priority_queue( const Compare& cmp = Compare(), const Container& c = Container() );
  priority_queue( input_iterator start, input_iterator end, const Compare& comp = Compare(), const Container& c = Container() );

Priority queues can be constructed with an optional compare function cmp' and an optional container c. If start and end are specified, the priority queue will be constructed with the elements between start and end.


push
Syntax:
  #include <queue>
  void push( const TYPE& val );

The function push() adds val to the end of the current priority queue.

For example, the following code uses the push() function to add ten integers to the end of a queue:

   queue<int> q;
   for( int i=0; i < 10; i++ )
     q.push(i);         

size
Syntax:
  #include <queue>
  size_type size() const;

The size() function returns the number of elements in the current priority queue.

Related topics:

top
Syntax:
  #include <queue>
  TYPE& top();

The function top() returns a reference to the top element of the priority queue.

For example, the following code removes all of the elements from a stack and uses top() to display them:

   while( !s.empty() ) {
     cout << s.top() << " ";
     s.pop();
   }            
Related topics: