×

C++ Queue

C++ queue: Queue in C++ is also a container adapter with the functionality of a queue. Queue is just the opposite of the stack in C++ because stack works on LIFO and queue in C++ works on FIFO we will discuss about in brief. Queue is also a part of standard template library (STL).

Definition of Queue:-

Queue in STL (standard template library) is a kind of adaptor container, so basically queue in C++ is designed or developed to operate in a FIFO way. That stands for first in and first out of the element. That means that we can insert the element at the one end of the container and extract the element from the other end of the container. We can also say that queue is a FIFO queue. Queue is a data structure.

Some other points about Queue:-

Here are some other important points that you should know about queue:

So, as we know that queue is container adapter. That means is the container consists of object which holds data of the same type, that type may also be user defined also. Queue is created with the help of different-different sequence containers. As container adapters does not support iterators that means we can’t use them for the data manipulation.

Queue allows us to use push() and pop() member functions for insertion and deletion of element in container.

Example of queue:-

To understand queue better let’s take a real life example:

Imagine you are planning to watch a movie and you want the back seat so you go to the ticket counter and stand there and after you stand there many people came and join the line (queue) after you. So when you have taken your ticket you are going to leave the queue and then all the person who joined after you also going to leave after taking their tickets.

So this is the best example of FIFO (first in first out) that means element is going to insert from the rear end and going to removed from the front end.

Syntax of Queue,

std::queue

To use the queue in our program we firstly need to add header file for queue which would be include<queue>.

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

Here T means type of the element contained, we can substitute T with any other data type also, and we even use T as user defined data type.

Container here is the underlying container object.

Queue has the following construction:

Explicit queue(cont Container &cnt = container());

The queue() constructor creates an empty queue. By default it uses a deque as a container, but a queue can only be accessed in a first in, first out manner. We can also use a list as a container for a queue. The container is held in a protected object called c of type container.

Member type:-

Here is some member type of queue mention below:

Member typeDefinition
size_typeSize_t
value_typeT (First parameter of the template).
container_typeSecond parameter of the template.
ReferenceValue_type&
Difference_typePtrdiff_t
Const_referenceConst value_type&

Member Functions:-

List of some member function of queue as follow:

FunctionDescription
emptyThis member function came in use when we need to check the emptiness of the queue. If the queue is empty then it returns as true and if it not empty then it retunes as false.
(constructor)This member function is used to create or construct the queue container.
SizeThis member function helps in retuning the size of the queue container. Element stored in queue is measured as its size.
frontThis member function plays a very important role because it helps in accessing the front element of the queue and all the deletion operations is being done on the front element.
BackThis member function is also very important because it helps in accessing the rear element of the queue and all the insertion operation is being done on the rear element of the queue.
PushThis member function is being used to insert the new element in the queue from the rear end.
PopThis member function is used to delete the element from the queue. (deletion done from the front end)
EmplaceThis member function helps in inserting new element in the queue over the current rear element.
SwapThis helps in interchanging the content of any two containers in the reference./ helps in exchanging the content of queues.
Relational operatorsIf the non member function needed the queues then they specifies the relational operators./ relational operators for the queue.
Uses allocator<queue>All the non member functions use this allocator for the queues./helps in using allocator for queue.

Related Topics

Containership in C++

In C++, it is possible to create an object in one class into another class, and that object is a member of another class. This relationship is known as a...

4 minutes read.

C++ Maximum Index Problem

Given an array A[] of positive integers. We will find the maximum of (j-i) such that i and j are the indexes of A[] and A[i] <= A[j], i<=j For...

5 minutes read.

Initialize Vector in C++

Initialize Vector in C++  The following comparison operators are defined for vector and those are given below. ==, <, <=, !=, >,>=  This allows you to access the element of a vector using...

3 minutes read.

Sliding Window Technique in C++

Sliding Window Technique or Window Sliding Technique is a computational technique that is mainly used to reduce the use of nested loop and replace it with a single loop. It...

4 minutes read.

Method overriding in C++

What is method overriding? Using the same function in derived class as their base class is referred to as function/method overriding in c++. Method overriding is an example of polymorphism. With...

2 minutes read.

How to calculate size of string in C++

What is string in C++? In C++, a string is a sequence of characters. The string data type is part of the Standard Template Library (STL) and is defined in the...

4 minutes read.

Print Table Using For-loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

3 minutes read.

Dynamic _Cast in C++

C++ is one of the most powerful programming languages. We can write object-oriented and structured programming with the help of C++. In this article, we will learn about the dynamic...

3 minutes read.

C++ Call by Reference

Call by Reference is a C++ method for passing arguments to a function that enables us to pass the actual memory address of the parameter rather than a copy of...

4 minutes read.

Constexpr in C++

Constexpr is a keyword in C++ that is used to declare variables or functions as compile-time constants. This means that the value of a constexpr variable or the return value...

3 minutes read.

Abstract class in C++

In this article, you will get exposure to an abstract class in C++. We will discuss this topic using some practical examples too. To understand the abstract classes, you should...

6 minutes read.

Exception Handling in C++ vs Java

Nowadays, exception handling is a feature found in virtually all object-oriented languages. We can also find this type of feature in Java and C++. The try-catch and block is required...

3 minutes read.

Program to find the GCD of two numbers in C++

Before understanding the program of GCD or HCF, one must know what GCD or HCF is. What is GCD? The GCD is referred to as Greatest Common Divisor. HCF is the other...

8 minutes read.

Lexicographically Next Permutation in C++

In this tutorial, we'll look at how to use C++ to generate the lexicographically next permutation of a string. The lexicographically next permutation is the larger permutation. "ACB," for example,...

3 minutes read.

Object in C++

In this article, we will learn about Object in C++. In short, an object is a stateful entity with behaviour. Data is referred to as state, and functionality is referred to...

3 minutes read.

How to improve programming skills in C++

Before getting started, one should know why to improve their programming skills. To become a good software developer or programmer, one must be skilled in at least one programming language. Many...

4 minutes read.

Malloc() and new in C++

In C ++, malloc () and new are used for the same thing. During runtime, they are used to allocate memory. Malloc () and the new, on the other hand,...

4 minutes read.

This keyword in C++

This keyword in C++ is a pointer which points to the object. It is passed as an argument to functions which helps in accessing the object. It is used for a...

3 minutes read.

C++ Mutable keyword

C++ mutable keyword Mutable class storage specifier in C++ Storage class specifiers in C are auto, register, static, and external. Typedef is also considered as a specifier of the storage...

4 minutes read.

Advanced C++ with Boost Library

The goal of the Boost Libraries is to be widely applicable and used in a variety of applications. For instance, they can in handy when working with huge numbers whose...

4 minutes read.