-
Hajipur, Bihar, 844101
A queue in C++ is a linear data structure that follows the First In, First Out (FIFO) principle. This means the element that is added first will be the first one to be removed. You can think of it like a real-life queue of people waiting at a ticket counter — the person who arrives first is served first.
In C++, queues are implemented through the Standard Template Library (STL) and are defined in the <queue> header. The queue class is an adapter that works on top of other containers like deque or list.
Queues are widely used in programming for situations where you need to manage data in the order it arrives. Common real-world and technical examples include:
CPU task scheduling
Printer job queues
Breadth-first search (BFS) in graphs
Handling requests in order of arrival
Queues make sure that each element is processed in a fair and sequential manner.
To create a queue, include the <queue> header and declare it using std::queue.
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int> numbers;
numbers.push(10);
numbers.push(20);
numbers.push(30);
cout << "Front element: " << numbers.front() << endl;
cout << "Back element: " << numbers.back() << endl;
return 0;
}
Explanation:
Here, we declared a queue named numbers. Elements are added using push(), and we used front() and back() to access the first and last elements respectively.
The C++ STL queue supports several key operations for working with elements efficiently.
Adds a new element at the back of the queue.
numbers.push(40);
Removes the element at the front of the queue.
numbers.pop();
Returns the first element (the one to be removed next).
cout << numbers.front();
Returns the last element (the most recently added one).
cout << numbers.back();
Checks if the queue is empty.
if (numbers.empty()) cout << "Queue is empty";
Returns the total number of elements in the queue.
cout << "Queue size: " << numbers.size();
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<string> q;
q.push("Aditi");
q.push("Meera");
q.push("Riya");
cout << "Front: " << q.front() << endl;
cout << "Back: " << q.back() << endl;
q.pop();
cout << "After one pop, front is: " << q.front() << endl;
return 0;
}
Output:
Front: Aditi
Back: Riya
After one pop, front is: Meera
Explanation:
The first element (“Aditi”) entered first and was removed first when we used pop() — this perfectly demonstrates the FIFO principle.
Unlike vectors or lists, queues do not support random access or iteration. You can only access the front and back elements directly. If you need to view all elements, you can pop them one by one in a loop.
while (!q.empty()) {
cout << q.front() << " ";
q.pop();
}
By default, queues use deque as the underlying container. However, you can also use a list for customized implementations.
queue<int, list<int>> customQueue;
This allows flexibility depending on how you want to manage memory or access behavior.
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<string> tickets;
tickets.push("Customer 1");
tickets.push("Customer 2");
tickets.push("Customer 3");
while (!tickets.empty()) {
cout << tickets.front() << " is being served." << endl;
tickets.pop();
}
return 0;
}
Output:
Customer 1 is being served.
Customer 2 is being served.
Customer 3 is being served.
Explanation:
The first customer enters first and is served first, just like a real queue.
Maintains the order of data (FIFO).
Simple and easy to implement.
Ideal for managing real-time data flow.
Useful in simulations, scheduling, and messaging systems.
No random access to elements.
Traversing all elements requires removing them.
Slower than arrays for random reads or writes.
Use queues when:
Data must be processed in the same order it arrives.
You’re implementing scheduling systems or task management.
You need temporary holding before sequential processing.
A C++ Queue is a powerful STL container designed for FIFO data management. It simplifies sequential processing, especially in systems where order matters. With simple operations like push(), pop(), front(), and back(), queues are both easy to use and efficient in handling structured data flow.
Write a C++ program to create a queue of integers and perform basic operations like push(), pop(), and front().
Create a program that takes 5 names as input and stores them in a queue. Display all names in the order they were entered.
Write a program to count the total number of elements in a queue using the size() function.
Build a C++ program that removes all elements from a queue and checks whether it’s empty.
Write a program that simulates a queue system for customers waiting at a bank counter.
Create a program that reverses the elements of a queue using a stack.
Write a C++ program to display all elements of a queue without modifying its order.
Build a program that adds integers to a queue and removes only even numbers, displaying the remaining elements.
Write a program that uses a queue to perform Breadth-First Search (BFS) on a simple graph.
Create a C++ program that uses a queue to manage print jobs in order of arrival.