c++ stl priority_queue is a queue like data structure which gives largest element by default on top() operation. It requires #include <queue> header in the program. The look up time for largest or smallest object containing inside priority_queue is always constant. The priority_queue is also known as container adaptor.

c++stl priority queue and its application

 C++ stl priority queue example:-

c-stl-priority-queueFrom output of the above program it is clear that priority queue returns largest element by default. If you want to change the default behavior  i.e if you need to return smallest element then we need to provide a user defined comparator in priority queue.  Let us see below example.


C++ stl priority queue and its application

  1. Dijkstra algorithm uses priority queue to reduce time complexity.
  2. Prims algorithm for mst ( minimum spanning tree).
  3. Best First search algorithm like A * search algorithm.
  4. Bandwidth management.