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,
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.
Here is some member type of queue mention below:
|value_type||T (First parameter of the template).|
|container_type||Second parameter of the template.|
List of some member function of queue as follow:
|empty||This 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.|
|Size||This member function helps in retuning the size of the queue container. Element stored in queue is measured as its size.|
|front||This 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.|
|Back||This 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.|
|Push||This member function is being used to insert the new element in the queue from the rear end.|
|Pop||This member function is used to delete the element from the queue. (deletion done from the front end)|
|Emplace||This member function helps in inserting new element in the queue over the current rear element.|
|Swap||This helps in interchanging the content of any two containers in the reference./ helps in exchanging the content of queues.|
|Relational operators||If 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.|