Module 8 · The Standard Template Library — topic 4 of 7
Stacks, Queues and Priority Queues
Container adaptors give restricted interfaces: LIFO stacks, FIFO queues and priority queues. Use them for bracket matching, BFS, scheduling and top-k problems.
Learning objectives
- Use stack, queue and priority_queue operations
- Apply stacks and queues to classic problems
- Build a min-heap with a custom comparator
Definition
std::stack(LIFO),std::queue(FIFO) andstd::priority_queue(highest-priority-first) are container adaptors that provide a limited interface over an underlying container (deque by default, vector for priority_queue).
Introduction
Many algorithms only need the most recent item (undo, matching brackets, depth-first search) or the oldest item (print queues, breadth-first search) or the most important item (task scheduling). Adaptors give you exactly those operations and nothing that could misuse the data.
An adaptor is not a new data structure: it wraps an existing container (a deque by default for stack and queue, a vector for priority_queue) and exposes a deliberately small interface. You can choose the underlying container as a template argument when you need different performance characteristics.
Why it matters
Stacks and queues are fundamental data structures in DSA courses and interviews.
priority_queue solves scheduling and top-k problems in O(log n) per operation.
Detailed explanation
stack: push, pop, top, empty, size. queue: push, pop, front, back. priority_queue: push, pop, top (largest by default).
pop() returns void; read top()/front() first. Calling top/pop on an empty adaptor is undefined behaviour.
Min-heap: std::priority_queue<int, std::vector<int>, std::greater<int>>. Custom ordering uses a comparator that returns true when the first argument has lower priority.
None of the adaptors supports iteration.
Syntax
- Include
<stack>and<queue>. - Always check
empty()before top/front/pop. - priority_queue is a binary heap.
Code examples
Balanced brackets with a stack
{[()]} 1
([)] 0
(( 0A FIFO queue
printing report.pdf printing photo.png printing notes.txt
More examples
Task scheduling with a priority queue
1 fix server 1 pay fees 2 reply email 3 write notes smallest: 2
Step-by-step explanation
- The comparator returns true when
ashould come out afterb. - Priority 1 tasks come first; among equals, earlier arrivals win.
std::greater<int>turns the default max-heap into a min-heap.- Each push/pop is O(log n).
Expected output
Running “Task scheduling with a priority queue” prints exactly:
Practical applications
- Compilers use stacks to parse nested expressions.
- Breadth-first search uses a queue.
- Operating systems and Dijkstra's algorithm use priority queues.
Best practices
- Check empty() before top/front/pop.
- Use a named comparator struct for complex priorities.
- Add a sequence number to make heap ordering stable.
Common mistakes
Expecting pop() to return the value
Avoid
Prefer
pop returns void in all adaptors.
Popping an empty stack
Avoid
Prefer
top/pop on an empty adaptor is undefined behaviour.
Important notes
- priority_queue is not stable by itself; equal elements may come out in any order unless you add a tie-breaker.
std::dequecan serve as both a stack and a queue when you need iteration.
Key takeaways
- stack = LIFO, queue = FIFO, priority_queue = best first.
- pop() returns void.
- Use greater<> or a comparator for min-heaps.
Try it yourself
- 1Evaluate a postfix expression like "3 4 + 2 *" with a stack.
- 2Find the k largest numbers in a vector using a min-heap of size k.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
5 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerWhat does `std::priority_queue<int>` return from top() by default?