Queue Basics in C++
C++ queue basics with FIFO order: push to the back, read front and back, pop from the front, and process a task list the same way BFS walks a graph.
1#include <iostream>2#include <queue>3using namespace std;45int main() {6 // Create queue7 queue<int> q;89 // Enqueue elements10 q.push(10);11 q.push(20);12 q.push(30);13 q.push(40);14 q.push(50);1516 cout << "Queue size: " << q.size() << endl;17 cout << "Front element: " << q.front() << endl;18 cout << "Back element: " << q.back() << endl;1920 // Display queue (by dequeuing)21 cout << "\nQueue elements (FIFO - First In First Out):" << endl;22 while (!q.empty()) {23 cout << q.front() << " ";24 q.pop();25 }26 cout << endl;2728 // Queue for BFS-like processing29 queue<string> taskQueue;3031 taskQueue.push("Task 1");32 taskQueue.push("Task 2");33 taskQueue.push("Task 3");3435 cout << "\nProcessing tasks:" << endl;36 while (!taskQueue.empty()) {37 cout << "Processing: " << taskQueue.front() << endl;38 taskQueue.pop();39 }4041 return 0;42}
Sample Output
Front element: 10
Oldest item.
Back element: 50
Newest item.
In this one
- What FIFO order means
- How front(), back(), and pop() work together
- Why a queue keeps items in arrival order
- How queues drive task processing and BFS
How it works
- 1
Push 10 to 50; size = 5, front = 10, back = 50.
- 2
Print front and pop until empty: 10 20 30 40 50.
- 3
Push Task 1, Task 2, Task 3.
- 4
Process each from the front.
Edge cases
front() on an empty queue
Call q.front() after the loop.
Undefined behavior. Always test empty() first.
Adding while processing
Push a new task inside the processing loop.
It joins the back and is processed after the current ones. That is exactly how BFS explores level by level.
What's going on
A queue adds at the back and removes from the front, like a line at a ticket counter. The first item in is the first out, called FIFO (first in, first out).
push adds 10 to 50 at the back. size() is 5, front() is 10 (the oldest), and back() is 50 (the newest).
Like a stack, a queue cannot be looped over directly. The display loop prints front() and then calls pop() to remove it, until the queue is empty. The output keeps the original order, 10 20 30 40 50, which is the difference from a stack.
The second part is the typical use: a line of work items. Three task names are pushed, then processed one at a time from the front. The same pattern drives breadth-first search on a graph, print spoolers, and request handling on a server. In BFS you would push new neighbours onto the back while processing from the front.
pop() returns nothing, just as with stack, so read front() first. Calling front() or pop() on an empty queue is undefined behavior, which is why the loops test empty().
All of these operations are O(1). queue is an adapter that uses a deque underneath by default.
The headers come first. <iostream> is there because it provides cout and cin for input/output operations. <queue> is there because it provides queue container class. Leave one out and the compiler stops at the first name it does not recognize.
Understanding Queue
FIFO Concept:
Key Features:
Queue Operations
push() (Enqueue):
q.push(10); // Add to back
pop() (Dequeue):
q.pop(); // Remove from front
front():
int value = q.front(); // Access front element
back():
int value = q.back(); // Access back element
What happens here:
Queue Properties
Size and Empty:
q.size() // Number of elements
q.empty() // True if empty
Access:
Task Processing
Using Queue:
What happens here:
When to Use Queue
Good fits:
Example Scenarios:
Important Considerations
FIFO Order:
Performance:
Limitations:
return 0;
return 0; closes main and tells the operating system the program finished without an error.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Push A, B, C, then pop once. What is front()?
Pick an answer to check it.
Question 2
What does q.back() return after pushing 10, 20, 30?
Pick an answer to check it.
Questions people ask
Can I see the middle of a queue?
No. Use deque if you need index access.
What is a circular queue?
A fixed-size array where front and back wrap around. std::queue grows instead.
Which queue gives the largest item first?
priority_queue.