C++ Priority Queues

C++ Priority Queues are like queues, but it is specially designed such that the first element of the queue is the greatest of all elements in the queue and elements are in non-increasing order.

  1. It provides a constant-time lookup of the largest (by default) element, at the expense of logarithmic insertion and extraction.
  2. A user-provided comparison can be supplied to change the ordering. e.g. using std:: greater<T> would cause the smallest element to appear as the top().
  3. It is similar to managing heap in random order without invalidating it.

Priority Queue Constructors
  • empty - returns true(1) if the priority queue has no element else false(0).
  • pop - removes the top element of a priority queue
  • push - adds an element to the end of the priority queue
  • size - returns the number of items in the priority queue
  • top - returns the top element of the priority queue
Below is the implementation of the Priority Queue Constructors.

Reference: cppreference