Skip to content
C++ Better Explained
Go back
C++ Queue and Stack Tutorial: FIFO and LIFO Containers Explained
Edit page

C++ Queue and Stack Tutorial: FIFO and LIFO Containers

Two of the most fundamental data structures in programming are the queue and the stack. They both store a sequence of items, but they have completely opposite rules for which item comes out first. C++ provides both as ready-to-use containers in the Standard Library.


The Core Idea: LIFO vs FIFO

Stack (LIFO — Last In, First Out): Think of a stack of plates. You always add and remove from the top. The last plate you put on is the first one you take off.

Queue (FIFO — First In, First Out): Think of a queue at a checkout. The first person who joined is the first to be served. New people join at the back.


std::stack

std::stack is in the <stack> header. Its key operations are:

#include <iostream>
#include <stack>

int main() {
    std::stack<int> s;

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

    std::cout << "Top: " << s.top() << "\n"; // 30

    s.pop(); // removes 30
    std::cout << "Top: " << s.top() << "\n"; // 20

    std::cout << "Size: " << s.size() << "\n"; // 2
    return 0;
}

Iterating a Stack

There’s no iterator for std::stack. To process all elements, pop them one by one:

std::stack<int> s;
s.push(1); s.push(2); s.push(3);

while (!s.empty()) {
    std::cout << s.top() << " "; // 3 2 1
    s.pop();
}

Note they come out in reverse order — that’s LIFO.

Practical Use: Checking Balanced Brackets

Stacks are perfect for bracket-matching problems because you need to remember what you opened most recently:

#include <iostream>
#include <stack>
#include <string>

bool isBalanced(const std::string& expr) {
    std::stack<char> s;
    for (char c : expr) {
        if (c == '(' || c == '[' || c == '{') {
            s.push(c);
        } else if (c == ')' || c == ']' || c == '}') {
            if (s.empty()) return false;
            char top = s.top();
            s.pop();
            if ((c == ')' && top != '(') ||
                (c == ']' && top != '[') ||
                (c == '}' && top != '{')) {
                return false;
            }
        }
    }
    return s.empty();
}

int main() {
    std::cout << isBalanced("(a + [b * c])")   << "\n"; // 1 (true)
    std::cout << isBalanced("(a + [b * c)")    << "\n"; // 0 (false)
    std::cout << isBalanced("{[(hello)]}") << "\n"; // 1 (true)
    return 0;
}
If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

std::queue

std::queue is in the <queue> header. Its key operations are:

#include <iostream>
#include <queue>

int main() {
    std::queue<std::string> q;

    q.push("Alice");
    q.push("Bob");
    q.push("Carol");

    std::cout << "Front: " << q.front() << "\n"; // Alice
    std::cout << "Back:  " << q.back()  << "\n"; // Carol

    q.pop(); // removes Alice
    std::cout << "Front: " << q.front() << "\n"; // Bob

    std::cout << "Size: " << q.size() << "\n"; // 2
    return 0;
}

Iterating a Queue

Like stack, there’s no iterator — drain it with a loop:

std::queue<std::string> q;
q.push("Alice"); q.push("Bob"); q.push("Carol");

while (!q.empty()) {
    std::cout << q.front() << " "; // Alice Bob Carol
    q.pop();
}

They come out in insertion order — that’s FIFO.

Practical Use: Print Queue Simulation

#include <iostream>
#include <queue>
#include <string>

int main() {
    std::queue<std::string> printQueue;

    printQueue.push("report.pdf");
    printQueue.push("invoice.pdf");
    printQueue.push("photo.png");

    std::cout << "Processing print jobs:\n";
    int jobNum = 1;
    while (!printQueue.empty()) {
        std::cout << "Job " << jobNum++ << ": " << printQueue.front() << "\n";
        printQueue.pop();
    }
    return 0;
}

Output:

Processing print jobs:
Job 1: report.pdf
Job 2: invoice.pdf
Job 3: photo.png

Always Check empty() Before Accessing Elements

Calling top() on an empty stack or front() on an empty queue is undefined behavior — your program may crash or produce garbage output. Always guard with empty():

if (!myStack.empty()) {
    std::cout << myStack.top() << "\n";
}

if (!myQueue.empty()) {
    std::cout << myQueue.front() << "\n";
}

Quick Reference Comparison

Operationstd::stackstd::queue
Add elementpush(val) — goes to toppush(val) — goes to back
Remove elementpop() — removes toppop() — removes front
Peektop()front(), back()
OrderLIFO (Last In, First Out)FIFO (First In, First Out)
Header<stack><queue>

When to Use Each

Use a stack when:

Use a queue when:



Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Previous Post
C++ std::pair Explained: Store Two Values Together for Beginners
Next Post
C++ std::set Tutorial: Sorted Unique Collections for Beginners