×

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

Dynamic Memory Allocation in C++

In some programming situations, the number of data items changes as the program is running, which is known as dynamic data or input. Consider a real-world situation where a program...

3 minutes read.

How the value is passed in C++

Introduction: The call-by-value method of giving arguments to a function duplicates the real value of an argument into the formal parameter of the function. In this instance, modifications to the parameter...

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

How to Reverse a String in C++ using Do-While Loop

Strings In C++, a string is an object that represents a group (or sequence) of various characters. Strings are part of the standard string class in C++ (std::string). The characters of...

4 minutes read.

Single dimension array

C++ Array An array is a collection of data (elements) of the same data types. The elements of an array are allocated in contiguous memory allocation. Elements of the array are accessed through...

1 minute read.

Lambda Expression in C++

The lambda expression was introduced in C++ 11. It is used to write the inline function in C++. The code written in lambda expression cannot be reused further. The syntax...

3 minutes read.

Difference between OOP and POP in C++

Object-Oriented Programming (OOP) Prioritizes data over methods (functions) and treats data as an essential component of program development.  OOP prohibits the free flow of data throughout the system. Tighter ties to data manipulation...

4 minutes read.

Two dimension array

C++ Two dimension (2D) Array Two dimension (2D) array is an array of arrays. It is represented in the form of row and column. The elements of 2D array are accessed through the...

2 minutes read.

Constructor Vs Destructor in C++

C++ Constructor A function Object () { [native code] } is a member function that shares the same name as the class. It is called automatically whenever a class object is...

3 minutes read.

Bitmasking in C++

Bitmasking is a technique that is used to access a particular bit within the data bytes. When you apply the iterative approach, this phenomenon is used. A bitmask is described...

6 minutes read.

Handling multiple clients on the Server with multithreading using Socket Programming in C or C++

To understand this guide completely, the reader is assumed to be familiar with the foundations of server and client models and socket programming. If one wants to create any scalable...

7 minutes read.

C++ Aggregation

C++ Aggregation Definition: In C++, aggregation is a process in which one class (as an entity reference) defines another class. It provides another way to reuse the class. It represents...

4 minutes read.

C++ Bitset

Overview In C++, bitset represents a fixed-sequence of some bits values by either 0 and 1. Zero represents the value as false or unset, while 1 represents the value as true...

4 minutes read.

Returning a Function Pointer from a Function in C/C++

Pointers to functions can be used in the C programming language just like standard data pointers such as "int *," "char *," etc. The following is a basic example of a...

3 minutes read.

Object Slicing in C++

In this article, we will learn about Object slicing. When an object from a derived class is assigned to an object from a base class in C++, these extra attributes...

3 minutes read.

Desired Capabilities in Selenium Web Driver in C++

A class called Desired Capabilities is used to specify a set of fundamental requirements, such as browser, operating system, and version combinations.to carry out automated testing of a web application...

7 minutes read.

C++ User Defined Exceptions

Overriding and inheriting exception class capabilities may be used to define the new exception. Exception handling can also be used with classes. We may also make an exception for user-defined...

4 minutes read.

Classes and Objects in C++

When it comes to object-oriented programming, objects are the basic building blocks. Memory is taken up by objects, which contain data and methods or functions that operate on it. On...

3 minutes read.

Hexadecimal to Decimal in C++

In computers, hexadecimal numbers are represented with base 16 and decimal numbers are represented with base 10 and values 0-9, whereas hexadecimal numbers have digits ranging from 0 to 15,...

3 minutes read.

Division in C++

C++ Division Arithmetic Operation In C++ the arithmetic operator / is used for division. This operator takes two operands and returns the result of dividing the left operand by the right...

3 minutes read.