Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
A queue is a container in C++ STL (Standard Template Library) that follows FIFO (First In First Out) arrangement. FIFO arrangement basically means that the element inserted first is the one that is removed first. It resembles a queue in daily life. The elements are inserted from the back of the queue, and the elements are removed from the front of the queue.
A queue is declared by using the following syntax:
queue<object type> queue_name;
Now that we understand what a queue is, let's discuss the different methods of the queue and their syntax.
Methods of Queue
This section will discuss the different methods allowed for queue containers in STL.
push() and pop() The 'push()' method is used to insert an element into the queue from the back side, and the 'pop()' method is used to remove the first element on the front side
front() and back() The 'front()' method returns a reference to the first element of the queue, and the 'back()' method returns the reference to the last element of the queue.
size() and empty() The 'size()' method returns the size of the queue, and the 'empty()' method returns true if the queue is empty; else returns false.
swap() The 'swap()' method is used to exchange the elements of two queues of the same type. The sizes of the two queues may be different.
C++ code to demonstrate the methods of queue
// C++ program to demonstrate the methods of queue in STL
#include <iostream>
#include <queue>
using namespace std;
int main()
{
// Declaring a queue of integers
queue<int> q;
// Insert 2 in queue. After insertion q will be = {2}
q.push(2);
//Insert 3 in queue. After insertion q will be = {2, 3}
q.push(3);
//Insert 4 in queue. After insertion q will be = {2, 3, 4}
q.push(4);
// Print the size of the queue
cout<<"The size of the queue is: "<< q.size() <<endl;
// Print the first element at the front side of the queue
cout << "The first element at the front side of the queue is: " << q.front() <<endl;
// Print the first element at the back side of the queue
cout << "The first element at the back side of the queue is: " << q.back() <<endl;
// Pop all the elements of queue and print them
cout<<"Elements in the queue are:"<<endl;
while(!q.empty())
{
int x = q.front();
cout<<x<<endl;
q.pop();
}
// Declare two queues
queue<int> q1;
queue<int> q2;
// Insert 2, 3 and 4 in q1
q1.push(2);
q1.push(3);
q1.push(4);
// Insert 5, 6 and 7 in q2
q2.push(5);
q2.push(6);
q2.push(7);
cout<<"Before swapping the content of q1 and q2 - "<<endl;
// Print the first element at the front side of the q1
cout << "The first element at the front side of the q1 is: " << q1.front() <<endl;
// Print the first element at the back side of the q1
cout << "The first element at the back side of the q1 is: " << q1.back() <<endl;
// Print the first element at the front side of the q2
cout << "The first element at the front side of the q1 is: " << q2.front() <<endl;
// Print the first element at the back side of the q2
cout << "The first element at the back side of the q1 is: " << q2.back() <<endl;
// Swap the content of q1 and q2
q1.swap(q2);
cout<<"After swapping the content of q1 and q2 - "<<endl;
// Print the first element at the front side of the q1
cout << "The first element at the front side of the q1 is: " << q1.front() <<endl;
// Print the first element at the back side of the q1
cout << "The first element at the back side of the q1 is: " << q1.back() <<endl;
// Print the first element at the front side of the q2
cout << "The first element at the front side of the q1 is: " << q2.front() <<endl;
// Print the first element at the back side of the q2
cout << "The first element at the back side of the q1 is: " << q2.back() <<endl;
}
You can also try this code with Online C++ Compiler
Output
The size of the queue is: 3
The first element at the front side of the queue is: 2
The first element at the back side of the queue is: 4
Elements in the queue are:
2
3
4
Before swapping the content of q1 and q2 -
The first element at the front side of the q1 is: 2
The first element at the back side of the q1 is: 4
The first element at the front side of the q1 is: 5
The first element at the back side of the q1 is: 7
After swapping the content of q1 and q2 -
The first element at the front side of the q1 is: 5
The first element at the back side of the q1 is: 7
The first element at the front side of the q1 is: 2
The first element at the back side of the q1 is: 4
FAQs
What is queue? A queue is a container in C++ STL (Standard Template Library) that follows FIFO (First In First Out) arrangement.
What is meant by FIFO arrangement in queue? FIFO arrangement basically means that the element which is inserted first is the one that is removed first. It resembles a queue in daily life.
Key takeaways-
This article discussed the ‘queue’ container in C++ STL (Standard Template Library). If you think that this blog helped you share it with your friends!.