C++ Queue
Understanding First-In-First-Out (FIFO) containers
๐ถ What is C++ Queue?
C++ Queue is a container adapter that follows FIFO (First-In-First-Out) principle. Elements are added at the back and removed from the front, like people waiting in line.
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int> myQueue;
myQueue.push(10);
myQueue.push(20);
myQueue.push(30);
cout << "Front: " << myQueue.front() << endl;
return 0;
}
Output:
Front: 10
Key Queue Operations
Push
Add element to the back of queue
myQueue.push(42);
Pop
Remove element from front of queue
myQueue.pop();
Front & Back
Access front and back elements
int front = myQueue.front();
int back = myQueue.back();
Size & Empty
Check queue size and if empty
int size = myQueue.size();
bool isEmpty = myQueue.empty();
๐น Basic Queue Operations
Queues follow the FIFO (First-In, First-Out) principle, perfect for processing items in arrival
order. Key operations are push() to add at the back, front() to access the
next item, back() to see the last, and pop() to remove from the front. Queues are widely
used in task scheduling, breadth-first search, buffering, and any scenario requiring orderly processing. They ensure
fairness and predictable sequence handling.
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<string> customerQueue;
// Add customers to queue
customerQueue.push("Alice");
customerQueue.push("Bob");
customerQueue.push("Charlie");
cout << "Queue size: " << customerQueue.size() << endl;
cout << "Next customer: " << customerQueue.front() << endl;
cout << "Last customer: " << customerQueue.back() << endl;
// Serve customers (FIFO order)
cout << "\nServing customers:" << endl;
while(!customerQueue.empty()) {
cout << "Serving: " << customerQueue.front() << endl;
customerQueue.pop();
}
cout << "Queue is now empty: " << (customerQueue.empty() ? "Yes" : "No") << endl;
return 0;
}
Output:
Queue size: 3
Next customer: Alice
Last customer: Charlie
Serving customers:
Serving: Alice
Serving: Bob
Serving: Charlie
Queue is now empty: Yes
๐น Queue for Task Processing
Queues are essential for managing task processing systems where jobs must be handled sequentially in the
exact order they arrive. You enqueue tasks like "Send email", "Update database", or "Process payment"
and systematically dequeue them for processing. This FIFO (First-In, First-Out) approach guarantees that no task is
skipped and processing maintains strict arrival order. Implemented using std::queue or
std::deque, queues provide an efficient, reliable mechanism for building message systems, print
spoolers, and background job processors where task ordering is critical for correctness and fairness.
#include <iostream>
#include <queue>
#include <string>
using namespace std;
class TaskProcessor {
private:
queue<string> taskQueue;
public:
void addTask(string task) {
taskQueue.push(task);
cout << "Added task: " << task << endl;
}
void processNextTask() {
if(!taskQueue.empty()) {
string currentTask = taskQueue.front();
taskQueue.pop();
cout << "Processing: " << currentTask << endl;
} else {
cout << "No tasks to process" << endl;
}
}
void showPendingTasks() {
cout << "Pending tasks: " << taskQueue.size() << endl;
if(!taskQueue.empty()) {
cout << "Next task: " << taskQueue.front() << endl;
}
}
};
int main() {
TaskProcessor processor;
processor.addTask("Send email");
processor.addTask("Update database");
processor.addTask("Generate report");
processor.showPendingTasks();
processor.processNextTask();
processor.processNextTask();
processor.showPendingTasks();
return 0;
}
Output:
Added task: Send email
Added task: Update database
Added task: Generate report
Pending tasks: 3
Next task: Send email
Processing: Send email
Processing: Update database
Pending tasks: 1
Next task: Generate report
๐น Priority Queue
C++'s std::priority_queue orders elements so the highest-priority item is always accessible
first. By default, itโs a max-heap. You can customize it to be a min-heap using a different comparator.
Priority queues are crucial for algorithms requiring efficient extremum access, like Huffman coding, load balancing,
or event-driven simulations. They offer logarithmic time for insertion and deletion, making them more efficient than
repeatedly sorting a container.
#include <iostream>
#include <queue>
using namespace std;
int main() {
// Priority queue (max-heap by default)
priority_queue<int> pq;
pq.push(30);
pq.push(10);
pq.push(50);
pq.push(20);
cout << "Priority Queue (highest first):" << endl;
while(!pq.empty()) {
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
// Min-heap priority queue
priority_queue<int, vector<int>, greater<int>> minPQ;
minPQ.push(30);
minPQ.push(10);
minPQ.push(50);
minPQ.push(20);
cout << "Min Priority Queue (lowest first):" << endl;
while(!minPQ.empty()) {
cout << minPQ.top() << " ";
minPQ.pop();
}
return 0;
}
Output:
Priority Queue (highest first):
50 30 20 10
Min Priority Queue (lowest first):
10 20 30 50