Queues in C++ STL
A queue is a container that follows the First In, First Out (FIFO) principle. The first element you add is the first one to leave. Think of a queue at a ticket counter — the person who arrives first gets served first. Nobody jumps to the front.
C++ provides a built-in queue through the STL in the <queue> header. It also provides a priority queue, where elements are served based on priority rather than arrival order.
FIFO Principle — Visual Diagram
ENQUEUE (push) from the back:
FRONT → [10][20][30][40] ← BACK
DEQUEUE (pop) from the front:
After pop: [20][30][40] (10 removed first)
Enqueue order: 10 → 20 → 30 → 40
Dequeue order: 10 → 20 → 30 → 40 (same order)
Declaring a Queue
#include <queue>
using namespace std;
queue<int> q; // queue of integers
queue<string> tasks; // queue of strings
Core Queue Operations
| Operation | Method | Description |
|---|---|---|
| Enqueue | q.push(val) | Add element to the back |
| Dequeue | q.pop() | Remove element from the front |
| Front | q.front() | View the front element |
| Back | q.back() | View the back element |
| Empty check | q.empty() | Returns true if queue is empty |
| Size | q.size() | Returns number of elements |
Basic Queue Example
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int> q;
q.push(10);
q.push(20);
q.push(30);
cout << "Front: " << q.front() << endl; // 10
cout << "Back: " << q.back() << endl; // 30
cout << "Size: " << q.size() << endl; // 3
q.pop(); // removes 10
cout << "After pop, Front: " << q.front() << endl; // 20
return 0;
}
Output:
Front: 10
Back: 30
Size: 3
After pop, Front: 20
Iterating Through a Queue
Like the stack, the STL queue does not support iterators. You must pop elements one by one to visit them all.
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int> q;
q.push(5);
q.push(15);
q.push(25);
q.push(35);
cout << "Processing queue (front to back):" << endl;
while (!q.empty()) {
cout << q.front() << " ";
q.pop();
}
cout << endl;
return 0;
}
Output:
Processing queue (front to back): 5 15 25 35
Real-World Example: Print Job Queue
Printers use a queue to manage print jobs. The first document sent to the printer prints first.
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int main() {
queue<string> printQueue;
printQueue.push("Invoice.pdf");
printQueue.push("Report.docx");
printQueue.push("Photo.jpg");
cout << "Printing documents:" << endl;
int jobNum = 1;
while (!printQueue.empty()) {
cout << "Job " << jobNum++ << ": " << printQueue.front() << " - DONE" << endl;
printQueue.pop();
}
return 0;
}
Output:
Printing documents:
Job 1: Invoice.pdf - DONE
Job 2: Report.docx - DONE
Job 3: Photo.jpg - DONE
Priority Queue
A priority queue is a special queue where each element has a priority. The element with the highest priority is always at the front, regardless of insertion order. C++ STL includes priority_queue in the same <queue> header.
#include <iostream>
#include <queue>
using namespace std;
int main() {
priority_queue<int> pq; // max-heap by default
pq.push(30);
pq.push(10);
pq.push(50);
pq.push(20);
cout << "Processing by priority (highest first):" << endl;
while (!pq.empty()) {
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
return 0;
}
Output:
Processing by priority (highest first): 50 30 20 10
By default, the largest number has the highest priority (max-heap). Elements come out in descending order.
Min-Heap Priority Queue
To get the smallest element first, use a min-heap by changing the comparator.
#include <iostream>
#include <queue>
#include <vector>
#include <functional>
using namespace std;
int main() {
priority_queue<int, vector<int>, greater<int>> minPQ;
minPQ.push(30);
minPQ.push(10);
minPQ.push(50);
minPQ.push(20);
cout << "Processing smallest first:" << endl;
while (!minPQ.empty()) {
cout << minPQ.top() << " ";
minPQ.pop();
}
cout << endl;
return 0;
}
Output:
Processing smallest first: 10 20 30 50
Deque — Double-Ended Queue
A deque (double-ended queue) allows insertion and removal from both the front and the back. It is more flexible than a regular queue.
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> dq;
dq.push_back(20); // add to back
dq.push_back(30);
dq.push_front(10); // add to front
cout << "Front: " << dq.front() << endl; // 10
cout << "Back: " << dq.back() << endl; // 30
dq.pop_front(); // remove from front
dq.pop_back(); // remove from back
cout << "Middle remaining: " << dq.front() << endl; // 20
return 0;
}
Output:
Front: 10
Back: 30
Middle remaining: 20
Stack vs Queue vs Priority Queue
┌──────────────────┬──────────────┬────────────────────────────────┐
│ Container │ Order │ Use Case │
├──────────────────┼──────────────┼────────────────────────────────┤
│ stack │ LIFO │ Undo, backtracking, DFS │
│ queue │ FIFO │ Print jobs, BFS, task queues │
│ priority_queue │ By priority │ Scheduling, shortest path │
│ deque │ Both ends │ Sliding window, browser history│
└──────────────────┴──────────────┴────────────────────────────────┘
Common Queue Use Cases
| Use Case | How Queue Helps |
|---|---|
| Task scheduling | Tasks processed in the order they arrive |
| Print spooler | Documents printed in submission order |
| Breadth-first search (BFS) | Nodes explored level by level using a queue |
| Customer service line | First customer in gets served first |
| Network packet handling | Data packets processed in arrival order |
Key Takeaways
- A queue follows FIFO — the first element added is the first removed.
- Use
push()to enqueue at the back andpop()to dequeue from the front. - Always check
empty()before callingfront(),back(), orpop(). - A priority queue always serves the highest-priority element first (max-heap by default).
- A deque allows insertion and deletion at both ends.
