Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library

C++ <priority_queue>

A priority queue is a dynamic data structure that holds priority. It is similar to a heap, where elements can be inserted in any order and max heap element is retrieved first. Similarly, max heap element is deleted first.

Features of queue

  • It is a dynamic data structure.
  • It has dynamic size.
  • It uses dynamic memory allocation.


template < class T, class Container = vector<T>,
   class Compare = less<typename Container::value_type>> class priority_queue; 


T Type of the elements stored in the container.
Container Type of the underlying container object where the elements are stored.
Compare A binary predicate that takes two elements of set as arguments and returns a bool. Elements are sorted by using this function.

Member Types

Member typesDefinition
value_type T (First template parameter)
container_type Container (Second template parameter), deque<value_type>
reference value_type&
const_reference const value_type&
size_type size_t

C++ - Priority Queue Functions

The C++ priority_queue container has a number of member functions which are listed below:

Member Functions

empty() Checks whether the priority_queue is empty or not.
size() Returns the length of the priority_queue in terms of bytes.
top() Access top element of the priority_queue.
push() Adds a new element in the priority_queue.
pop() Deletes top element of the priority_queue.
emplace() Constructs and inserts a new element in the priority_queue
swap() Exchanges elements between two priority_queues.

Non-member function overloads

swap() Exchanges elements between two priority_queues.