C++ Standard Library C++ STL Library
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

C++ - <queue>



The C++ <queue> header file defines the queue and priority_queue container classes:

queue class

A queue is a linear dynamic data structure that follows First-In/First-Out (FIFO) principle. In a queue, addition of a new element and deletion of an element occurs at different end which implies that the element which is added first in the queue will be the first to be removed from the queue.

Features of queue

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

Syntax

template < class T, class Container = deque<T> > class queue; 

Parameters

T Type of the elements stored in the container.
Container Type of the underlying container object where the elements are stored.

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++ queue - Member Functions

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

Member Functions

FunctionsDescription
empty() Checks whether the queue is empty or not.
size() Returns the length of the queue in terms of bytes.
front() Access first element of the queue.
back() Access last element of the queue.
pop() Deletes first element of the queue.
push() Adds a new element at the end of the queue.
emplace() Constructs and inserts a new element at the end of the queue, after its current last element.
swap() Exchanges elements between two queues.

C++ queue - Non-member Functions

FunctionsDescription
operator == Checks whether two queues are equal or not.
operator != Checks whether two queues are unequal or not.
operator < Checks whether the first queue is less than the other or not.
operator > Checks whether the first queue is greater than the other or not.
operator <= Checks whether the first queue is less than or equal to the other or not.
operator >= Checks whether the first queue is greater than or equal to the other or not.
swap() Exchanges elements between two queues.

priority_queue class

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.

Syntax

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

Parameters

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 - Member Functions

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

Member Functions

FunctionsDescription
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.

C++ priority_queue - Non-member Functions

FunctionsDescription
swap() Exchanges elements between two priority_queues.

5