priority_queue<T>

The STL priority_queue is a container adapter that represents a collection of elements where only the largest element, determined by some comparison functor, can be accessed. Internally, the priority_queue is built on top of a heap using the heap algorithms make_heap, push_heap, and pop_heap.

priority_queue is a container adapter, not a true STL container, and is implemented in terms of another linear container class like vector or deque. To explicitly specify which container to use in the priority_queue, parametrize the priority_queue as priority_queue<T, ContainerType<T> >.

By default, the priority_queue sorts elements using the < operator. To change this behavior, parametrize the priority_queue as priority_queue<T, ContainerType<T>, ComparisonFunctor>. Note that you must specify the underlying container type in addition to the comparison functor class.

Common priority_queue<T> Member Functions
void push(const T& elem)
Insert element
myPQueue.push(137); // Push 137 onto the priority_queue

Enqueues a new element into the priority_queue.

void pop();
Remove maximal element
myPQueue.pop(); // Note: no return value

Dequeues the maximal element of the priority_queue. The behavior is undefined if the priority_queue has no elements, so be sure not to pop off of an empty priority_queue. Note that pop does not return a value; use top to get the value before popping.

const T& top() const
Return maximal element
int greatestElem = myPQueue.front();

Returns the maximal element of the priority_queue (the one that will be removed by pop). The behavior is undefined if the priority_queue has no elements, so be sure not to access the front element of an empty priority_queue.

size_type size() const
Get container size
int numElems = myPQueue.size();

Returns the number of elements stored in the priority_queue.

bool empty() const
Is priority_queue empty?
if(myPQueue.empty()) { ... }

Returns whether the priority_queue is empty. It's considered good practice to call empty to determine whether the priority_queue is empty rather than checking whether size() returns zero.

Other priority_queue<T> Member Functions
Constructor: priority_queue<T, Container<T>, CompareFn> (InputIterator start, InputIterator stop)
Constructs a new priority_queue with specified elements
priority_queue<int> myPQueue(myList.begin(), myList.end());

Constructs a new priority_queue whose contents are equal to the elements specified in the iterator range. The input range need not be sorted.