A Priority Queue is a variant of a Queue such that it’s elements are ordered based on their priority. C++ has a built-in priority queue data structure in it’s Standard Template Library (STL).

Priority queues are implemented as container adapters in C++.

This means that like other container adapters, they have member functions for the container objects.

Let’s understand how we can use the Priority Queue container in C++ to make our own priority queues.


Create a Priority Queue

We can create a priority queue by declaring a priority queue variable of the type std::priority_queue<type>.

The std:: namespace signifies that this supports STL (Standard Template Library) operations.

NOTE: To use this, we must include the <queue> header file in our program.

Since this is a container of the STL, we must provide the template type for the queue. It could be anything from int, float, char, string, etc.

For example, the following are valid declarations of a priority queue.

Common Methods on a Priority Queue

Some of the methods which the priority queue container supports are namely:

  • empty() -> Checks if the Priority Queue is empty
  • size() -> Returns the number of elements in the Queue
  • top() -> Returns the element at the top of the Queue
  • push() -> Pushes an element to the bottom of the Queue
  • pop() -> Pops the last element from the Queue

Let’s understand the above methods using an example. The below code uses all the above methods involving the priority queue.

Here, we create a priority queue of integers and insert the elements 100, 200, 400 and 50 in order.

Note that since a priority queue has the elements sorted in descending order from the top, the insertion will look like this:

Priority Queue

After pushing to the queue, we pop the topmost element from the Queue, i.e, the element with the highest priority.

Therefore, 400 will be popped out, making 200 as the new head of the Queue.

This shows how we can use the priority queue container easily. Let us move on to a case where you want to have your own priority scheme.


Implementing our own Comparison function

By default, the C++ priority queue evaluates priority only based on sorted values. We can change this, by implementing our own comparison function!

We can overload the default comparison function, by overloading the STL comparison operator.

For this, we must create a comparison class first. Let us call it CompareClass and then introduce our custom comparison in the operator() block.

This must return a bool, since this is a comparison function, and must also be public.

The code structure will look similar to this. We are now comparing based on the ascending order of values!

We are still not yet done. We need to make the compiler realize that we are using this new class for our comparison operator.

For that, we will need to add two more parameters to our priority_queue<> STL invocation.

The second parameter tells the compiler that the queue is implemented as a vector<type>. The third one is our Comparison Class.

We will modify our old code to include these new changes.

Output

Observe that we insert elements based on their increasing order of values now, so the top of the queue is the lowest element.

Thus, we have successfully implemented our own comparison function!


Conclusion

In this article, we looked at the C++ Priority Queue container. We also saw some of the methods involved in manipulating them.

Recommended Read: Hash Tables in C++


References


By admin

Leave a Reply

%d bloggers like this: