C++ Library - <priority_queue>
The C++ Priority_queue is a container adaptor from the STL that provides the dynamic priority queue data structure. It is implemented as a binary heap, allowing efficient insertion, removal and access to the highest or lowest priority elements. By default, it orders elements in descending order based on a comparator function.Priority queues are commonly used in algorithms like Dijkstra's shortest path and Prim's minimum spanning tree.
Syntax
Below is the syntax of std::priority_queue.
template <class T, class Container = vector<T>, class Compare = less<typename Container::value_type> < class priority_queue;
Parameters
T − Type of the element contained.
T may be substituted by any other data type including user-defined type.
Container − Type of the underlying container object.
Compare − Comparison object to be used to order the priority_queue.
This may be a function pointer or function object that can compare its two arguments.
Member types
Following member types can be used as parameters or return type by member functions.
| Sr.No. | Member types | Definition |
|---|---|---|
| 1 | value_type | T (First parameter of the template) |
| 2 | container_type | Second parameter of the template |
| 3 | size_type | size_t |
| 4 | reference | value_type& |
| 5 | const_reference | const value_type& |
| 6 | difference_type | ptrdiff_t |
Functions from <priority_queue>
Below is list of all methods from <priority_queue> .
Constructors
| Sr.No. | Method & Description |
|---|---|
| 1 | priority_queue::priority_queue default constructor Constructs an empty priority_queue with zero element. |
| 2 | priority_queue::priority_queue initialize constructor Constructs a priority_queue object and assigns internal container by a copy of ctnr. |
| 3 | priority_queue::priority_queue range constructor Constructs a priority_queue with as many elements in range of first to last. |
| 4 | priority_queue::priority_queue move constructor Constructs the priority_queue with the contents of other using move semantics. |
| 5 | priority_queue::priority_queue copy constructor Constructs a priority_queue with copy of each elements present in existing priority_queue other. |
Destructor
| Sr.No. | Method & Description |
|---|---|
| 1 | priority_queue::~priority_queue
Destroys priority_queue by deallocating container memory. |
Member functions
| Sr.No. | Method & Description |
|---|---|
| 1 | priority_queue::emplace
Constructs and inserts new element in sorted order in the priority_queue. |
| 2 | priority_queue::empty
Tests whether pritority_queue is empty or not. |
| 3 | priority_queue::operator= Assigns new contents to the priority_queue by replacing old ones. |
| 4 | priority_queue::pop
Removes front element of the priority_queue. |
| 5 | priority_queue::push Inserts new element in sorted order. |
| 6 | priority_queue::size
Returns the total number of elements present in the priority_queue. |
| 7 | priority_queue::swap
Exchanges the contents of priority_queue with contents of another priority_queue. |
| 8 | priority_queue::top
Returns a reference to the first element of the priority_queue |