C++ Tutorial Index

C++ Tutorial C++ History C++ Installation C++ First Program C++ cin and cout C++ Data type C++ Variable C++ operator C++ Keywords

C++ Control Statements

C++ If C++ Nested if C++ If-else C++ If-else-if C++ Switch C++ Break C++ Continue C++ Goto C++ For loop C++ While loop C++ Do while loop

C++ Functions

C++ Call by Value C++ Call by Reference C++ Recursion Function C++ Inline function C++ Friend function

C++ Arrays

Single dimension array Two dimension array

C++ Strings

C++ Strings

C++ Inheritance

C++ Inheritance Single level Inheritance Multilevel Inheritance Multiple Inheritance Hierarchical Inheritance Hybrid Inheritance

C++ Polymorphism

C++ Polymorphism C++ Overloading C++ Overriding C++ Virtual Function

C++ Pointers

C++ Pointers C++ this pointer

C++ Exception Handling

C++ Exception Handling

C++ Constructors

C++ Constructors Default Constructor Parameterize Constructor Copy constructor Constructor Overloading Destructor

C++ File Handling

C++ File Handling C++ Writing to file C++ Reading file C++ Close file

Miscellaneous

C Vs C++ C++ Comments C++ Data Abstraction C++ Identifier C++ Memory Management C++ Storage Classes C++ Void Pointer C++ Array To Function C++ Expressions C++ Features C++ Interfaces C++ Encapsulation std::min in C++ External merge sort in C++ Remove duplicates from sorted array in C++ Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++ C++ References C++ Friend Functions C++ Mutable keyword Unary Operators in C++ Initialize Array of objects with parameterized constructors in C++ Differences between #define & const in C/C++ C++ Program to Implement Shell Sort C++ Program to Implement Merge Sort Storage Classes in C Vector resize() in C++ Passing by Reference Vs. Passing by the pointer in C++ Free vs delete() in C++ goto statement in C and C++ C++ program to read string using cin.getline() C++ String Concatenation Heap Sort in C++ Swap numbers in C++ Input Iterators in C++ Fibonacci Series in C++ C ++ Program: Alphabet Triangle and Number Triangle C++ Program: Matrix Multiplication C++ Program to Print Fibonacci Triangle Stack in C++ Maps in C++ Queue in C++ C++ Bitset C++ Algorithms Priority Queue in C++ C++ Multimap C++ Deque Function Pointer in C++ Sizeof() Operators in C++ C++ array of Pointers free() Vs delete in C Timsort Implementation Using C++ CPP Templates C++ Aggregation C++ Enumeration C++ Math Functions C++ Object Class C++ Queue Initialize Vector in C++ Vector in C++ C++ STL Components Function overloading in C++ C++ Maximum Index Problem C++ find missing in the second array C++ Program to find the product array puzzle C++ Program To Find Largest Subarray With 0 Sum C++ Program To Move All Zeros To The End Of The Array C++ Program to find the element that occurs once C++ Program to find the largest number formed from an array Constructor Vs Destructor C++ Namespaces C++ OOPs Concept C++ Static C++ Structs C++ Try-Catch C++ User Defined Exceptions C++ Virtual Destructor C++ vs C# Malloc() and new in C++ Palindrome Number Program in C++ Snake Code in C++ Splitting a string in C++ Structure Vs Class in C++ Virtual Function Vs Pure Virtual Function C++ Bidirectional Iterators C++ Forward Iterators C++ Iterators C++ Output Iterators C++ Range-based For Loop Converting string into integer in C++ LCM Program in C++ Type conversion in C++ Add two numbers using the function in C++ Advantage and disadvantage friend function C++ Armstrong Number Program in C++ ATM machine program in C++ using functions Binary to Decimal in C++ Bit Manipulation in C++ C++ Constructor C++ Dijkstra Algorithm Using the Priority Queue C++ int into String C++ Signal Handling Decimal to Binary in C++ Decimal to Hexadecimal in C++ Decimal to Octal in C++ Factorial Program in C++ Function in C++ Hexadecimal to Decimal in C++ Octal to Decimal in C++ Reverse a Number in C++ Structure Vs Class in C++ C++ Forward Iterators C++ Output Iterators C++ Prime number program Char Array to String in C++ Constructor Overloading in C++ Default arguments in C++ Different Ways to Compare Strings in C++ Dynamic Binding in C++ Program to convert infix to postfix expression in C++ SET Data Structure in C++ Upcasting and Downcasting in C++ Reverse an Array in C++ Fast Input and Output in C++ Delete Operator in C++ Copy elision in C++ C++ Date and Time C++ Bitwise XOR Operator Array of sets in C++ Binary Operator Overloading in C++ Binary Search in C++ Implementing the sets without C++ STL containers Scope Resolution Operator in C++ Smart pointers in C++ Types of polymorphism in C++

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.



ADVERTISEMENT
ADVERTISEMENT