Container Adapters

Back

Loading concept...

๐Ÿ“š STL Container Adapters: Stack, Queue & Priority Queue

๐ŸŽญ The Restaurant Kitchen Story

Imagine youโ€™re running a busy restaurant kitchen. You have three special systems to organize your work:

  1. A stack of plates ๐Ÿฝ๏ธ - You put clean plates on top, and grab from the top
  2. A ticket line ๐ŸŽซ - First order in, first order served
  3. A VIP list โญ - Important customers always get served first

These three systems are exactly how C++ Container Adapters work!


๐Ÿฅž Stack Container: The Plate Stack

What is a Stack?

Think of a stack of pancakes. You can only:

  • Add a new pancake on top
  • Remove a pancake from the top
  • Look at the pancake on top

This is called LIFO - Last In, First Out!

graph TD A["Push 10"] --> B["Push 20"] B --> C["Push 30"] C --> D["Top = 30"] D --> E["Pop removes 30"] E --> F["Top = 20 now"]

Creating a Stack

#include <stack>
using namespace std;

// Empty stack of integers
stack<int> plates;

// Stack from another container
deque<int> d = {1, 2, 3};
stack<int> s(d);

Stack Operations

Operation What it does Example
push(x) Add on top s.push(5);
pop() Remove from top s.pop();
top() See the top int x = s.top();
empty() Is it empty? if(s.empty())
size() How many? int n = s.size();

๐ŸŽฎ Real Example: Undo Button

stack<string> actions;

// User types
actions.push("Hello");
actions.push("Hello World");

// User clicks Undo!
actions.pop();
// Now back to "Hello"

โš ๏ธ Important Rules

  1. Never call top() or pop() on empty stack!
  2. pop() doesnโ€™t return the value - use top() first
  3. Default uses deque underneath
// WRONG - will crash! โŒ
stack<int> s;
int x = s.top(); // Empty stack!

// RIGHT โœ…
if (!s.empty()) {
    int x = s.top();
    s.pop();
}

๐Ÿšถ Queue Container: The Waiting Line

What is a Queue?

Think of a line at the ice cream shop.

  • First person in line โ†’ gets served first
  • New people join at the back
  • People leave from the front

This is called FIFO - First In, First Out!

graph LR A["Back"] --> B["3"] B --> C["2"] C --> D["1"] D --> E["Front"] F["New person joins back"] -.-> A E -.-> G["First person leaves"]

Creating a Queue

#include <queue>
using namespace std;

// Empty queue
queue<string> line;

// Queue from deque
deque<int> d = {1, 2, 3};
queue<int> q(d);

Queue Operations

Operation What it does Example
push(x) Add to back q.push("Bob");
pop() Remove from front q.pop();
front() See the front string s = q.front();
back() See the back string s = q.back();
empty() Is it empty? if(q.empty())
size() How many? int n = q.size();

๐ŸŽฎ Real Example: Print Jobs

queue<string> printer;

// Add print jobs
printer.push("Report.pdf");
printer.push("Photo.jpg");
printer.push("Letter.doc");

// Process in order
while (!printer.empty()) {
    cout << "Printing: "
         << printer.front() << endl;
    printer.pop();
}
// Prints: Report, Photo, Letter

๐Ÿ”„ Stack vs Queue

Feature Stack ๐Ÿฅž Queue ๐Ÿšถ
Order LIFO FIFO
Add Top Back
Remove Top Front
Use case Undo, Back button Lines, Tasks

โญ Priority Queue: The VIP List

What is a Priority Queue?

Imagine a hospital emergency room:

  • Critical patients โ†’ treated first
  • Minor injuries โ†’ wait longer
  • Itโ€™s not about who came first, itโ€™s about priority!

By default, the biggest number has highest priority.

graph TD A["Add: 10, 30, 20"] --> B["Internal heap"] B --> C["Top = 30 highest!"] C --> D["Pop removes 30"] D --> E["Top = 20 now"]

Creating a Priority Queue

#include <queue>
using namespace std;

// Empty - max priority default
priority_queue<int> pq;

// Min priority - smallest first
priority_queue<int,
    vector<int>,
    greater<int>> minPQ;

Priority Queue Operations

Operation What it does Example
push(x) Add element pq.push(5);
pop() Remove highest pq.pop();
top() See highest int x = pq.top();
empty() Is it empty? if(pq.empty())
size() How many? int n = pq.size();

๐ŸŽฎ Real Example: Task Manager

priority_queue<int> tasks;

tasks.push(3);  // Low priority
tasks.push(10); // HIGH priority
tasks.push(5);  // Medium

// Process by priority
while (!tasks.empty()) {
    cout << tasks.top() << " ";
    tasks.pop();
}
// Output: 10 5 3

๐Ÿ”„ Min Priority Queue

Want smallest first? Flip it!

// Smallest number = highest priority
priority_queue<int,
    vector<int>,
    greater<int>> minHeap;

minHeap.push(30);
minHeap.push(10);
minHeap.push(20);

cout << minHeap.top(); // 10 (smallest!)

๐Ÿ† Custom Priority

For custom objects, define comparison:

struct Task {
    string name;
    int urgency;
};

// Compare by urgency
auto cmp = [](Task a, Task b) {
    return a.urgency < b.urgency;
};

priority_queue<Task,
    vector<Task>,
    decltype(cmp)> taskQ(cmp);

๐ŸŽฏ Quick Comparison Table

Feature Stack Queue Priority Queue
Order LIFO FIFO By Priority
Add push() push() push()
Remove pop() pop() pop()
Access top() front()/back() top()
Underlying deque deque vector + heap

๐Ÿ’ก When to Use What?

Use Stack when:

  • ๐Ÿ”™ Undo/Redo functionality
  • ๐Ÿ“‚ Back button navigation
  • ๐Ÿงฎ Expression evaluation
  • ๐Ÿ”„ Recursion simulation

Use Queue when:

  • ๐Ÿ–จ๏ธ Print job scheduling
  • ๐Ÿ“จ Message queues
  • ๐ŸŒŠ Breadth-first search
  • โณ Task scheduling (FIFO)

Use Priority Queue when:

  • ๐Ÿฅ Emergency room triage
  • ๐Ÿ“Š Top-K problems
  • ๐Ÿ—บ๏ธ Dijkstraโ€™s algorithm
  • โฐ Event scheduling by time

๐Ÿš€ Pro Tips

  1. Check before access! Always verify !empty() before top() or front()

  2. Remember: pop() returns nothing - it just removes!

  3. Default priorities:

    • priority_queue โ†’ biggest first (max-heap)
    • Use greater<int> for smallest first
  4. Theyโ€™re adapters! They wrap other containers:

    • Stack โ†’ wraps deque (default)
    • Queue โ†’ wraps deque (default)
    • Priority Queue โ†’ wraps vector (default)

๐ŸŽ‰ You Did It!

You now understand the three container adapters:

  • Stack = Plates (LIFO) ๐Ÿฅž
  • Queue = Line (FIFO) ๐Ÿšถ
  • Priority Queue = VIP (Highest first) โญ

These simple containers solve so many real problems. Every time you use an Undo button, process a queue of tasks, or sort by importance - youโ€™re using these concepts!

Go build something amazing! ๐Ÿš€

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.