Last Updated on July 20, 2022 by Ria Pathak
Queue:
The queue is a linear data structure that works on the principle of First in First out (FIFO). In the queue, the element which is added at least recently is removed first from it. For example, a queue of consumers for a resource or service where the consumer who comes first will be served first.
Given below are the operations of the queue:

Enqueue: This function is used to add an element to the rear end of the queue. If the queue is completely filled, then it will be in an overflow condition. The time complexity of the enqueue is O(1).

Dequeue: This function is used to remove an element from the front end of the queue. If the queue is empty, then it will be in an underflow condition. The time complexity of the dequeue is O(1).

Front: This function returns the front element of the queue. The time complexity of this function is O(1).

Rear: This function returns the last element of the queue. The time complexity of this function is O(1).
Queue STL:
In C++, the STL queue provides the functionality of a queue data structure.
A Queue is a linear data structure. Queue follows the FIFO rule i.e. First in First out. In other words we can say the element that goes in first is the element that comes out first.
For Example A ticket Queue outside a cinema hall where the person enters the queue first will get the ticket first.
Create STL queue in C++:

First we need to include a queue header file.
#include <queue>

After importing the header file we’ll create the queue with a suitable data type.
queue<type> q
For example:
queue<int> q;
queue<string> q1;
C++ queue STL Operations:
push(): This operation is used to insert an element at the end of the queue.
pop(): This operation is used to remove an element from the end of the queue.
front(): This operation is used to return the first element of the queue.
back(): This operation is used to return the last element of the queue.
empty(): This operation is used to check whether the queue is empty or not.
size(): This operation is used to return the size of the queue.
#include <iostream> #include <queue> using namespace std; // Print the queue void print(queue<int> q) { queue<int> g = q; while (!g.empty()) { cout << '\t' << g.front(); g.pop(); } cout << '\n'; } int main() { queue<int> q; q.push(10); q.push(20); q.push(30); cout << "The queue q is : "; print(q); cout << "\nq.size() : " << q.size(); cout << "\nq.front() : " << q.front(); cout << "\nq.back() : " << q.back(); cout << "\nq.pop() : "; q.pop(); print(q); return 0; }
Output:
The queue q is : 10 20 30
q.size() : 3
q.front() : 10
q.back() : 30
q.pop() : 20 30
Complexity
Time Complexity of various operations:
 push(): O(1)
 pop(): O(1)
 size(): O(1)
 front(): O(1)
 back(): O(1)
 empty(): O(1)
This article tried to discuss Queue C++ STL. Hope this blog helps you understand the concept. To practice problems feel free to check MYCODE  Competitive Programming.