Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🔴 Advanced  ·  Lesson 56

Stack, Queue and Priority Queue in C++ STL

In this lesson: Use stack, queue and priority_queue in C++ STL — push, pop, top, front — with examples and output. In Hindi & English.

Container adapters

stack, queue and priority_queue are container adapters: they sit on top of another container (usually deque or vector) and allow only a restricted set of operations. The restriction is the point — it makes your intent clear and prevents misuse.

AdapterRuleHeaderAddRemoveLook at
stackLIFO — last in, first out<stack>push(x)pop()top()
queueFIFO — first in, first out<queue>push(x) at backpop() from frontfront(), back()
priority_queueHighest priority first<queue>push(x)pop()top()

All three also have size() and empty(). None of them can be iterated with a range-based for.

std::stack — LIFO

Think of a pile of plates: you add and remove from the top only.

C++
#include <iostream>
#include <stack>

int main() {
    std::stack<int> s;
    s.push(10);
    s.push(20);
    s.push(30);

    std::cout << "top: " << s.top() << ", size: " << s.size() << "\n";
    while (!s.empty()) {
        std::cout << s.top() << " ";   // read first
        s.pop();                        // then remove
    }
    std::cout << "\n";
    return 0;
}
top: 30, size: 3 30 20 10

Practical use: balanced brackets

C++
#include <iostream>
#include <stack>
#include <string>

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

int main() {
    std::cout << balanced("{a[b(c)d]e}") << " "
              << balanced("(a[b)c]") << " "
              << balanced("((a)") << "\n";
    return 0;
}
1 0 0

Other stack uses: undo/redo, back button in a browser, function calls (the call stack), evaluating postfix expressions, depth-first search.

std::queue — FIFO

Think of a line at the fee counter: the first person to join is the first served.

C++
#include <iostream>
#include <queue>
#include <string>

int main() {
    std::queue<std::string> counter;
    counter.push("Aman");
    counter.push("Priya");
    counter.push("Ravi");

    std::cout << "first: " << counter.front()
              << ", last: " << counter.back() << "\n";

    while (!counter.empty()) {
        std::cout << "Serving " << counter.front() << "\n";
        counter.pop();
    }
    return 0;
}
first: Aman, last: Ravi Serving Aman Serving Priya Serving Ravi

Other queue uses: print queues, task scheduling, breadth-first search (BFS), buffering data between a producer and a consumer.

std::priority_queue — highest first

A priority_queue always gives you the largest element (a max-heap). push and pop are O(log n); top is O(1).

C++
#include <iostream>
#include <queue>
#include <vector>
#include <functional>

int main() {
    std::priority_queue<int> maxPq;                     // largest on top
    std::priority_queue<int, std::vector<int>,
                        std::greater<int>> minPq;       // smallest on top

    for (int m : {66, 91, 78, 55, 84}) {
        maxPq.push(m);
        minPq.push(m);
    }

    std::cout << "Top 3 marks: ";
    for (int i = 0; i < 3; i++) { std::cout << maxPq.top() << " "; maxPq.pop(); }

    std::cout << "\nLowest mark: " << minPq.top() << "\n";
    return 0;
}
Top 3 marks: 91 84 78 Lowest mark: 55

Priority queue of pairs or objects

Pairs compare by first, then second, so priority_queue<std::pair<int, std::string>> orders by marks. For your own class, give a comparison:

C++
struct Patient { std::string name; int severity; };
auto cmp = [](const Patient& a, const Patient& b) { return a.severity < b.severity; };
std::priority_queue<Patient, std::vector<Patient>, decltype(cmp)> er(cmp);

Uses: hospital emergency queues, Dijkstra's shortest path, picking top-K items, CPU scheduling.

stack vs queue vs priority_queue

Pointstackqueuepriority_queue
Order outNewest firstOldest firstHighest priority first
Read nexttop()front()top()
Default containerdequedequevector (as a heap)
push / pop costO(1)O(1)O(log n)
Real-life picturePile of platesQueue at a counterHospital emergency ward

Common mistakes

  • Writing int x = s.pop(); — pop() returns nothing. Read top()/front() first.
  • Calling top(), front() or pop() on an empty adapter — undefined behaviour. Check empty().
  • Expecting priority_queue to be sorted when printed — only top() is guaranteed to be the largest.
  • Forgetting std::greater<int> needs <functional> (usually included via <queue>, but include it to be safe).
  • Trying for (int x : myStack) — adapters have no iterators.
🏋️ Practice

(1) Reverse a string using a stack<char>. (2) Simulate a printer with a queue<string> of document names. (3) Read 10 marks and print the three lowest using a min-heap priority_queue.

Summary

  • stack = LIFO with push/pop/top; queue = FIFO with push/pop/front/back.
  • priority_queue returns the largest element by default; use std::greater for a min-heap.
  • pop() returns void — read the element first.
  • Always check empty() before top(), front() or pop().
  • They are adapters over deque/vector and cannot be iterated.

Related: STL introduction, list and deque, C: stack using array, C: queue.

Frequently Asked Questions

What is the difference between stack and queue in C++?
A stack is LIFO (last in, first out): you push and pop at the same end and read the element with top(). A queue is FIFO (first in, first out): you push at the back, pop from the front, and read it with front(). Both are container adapters in the STL.
Why does pop() not return the element in C++ STL?
pop() returns void for safety: if copying the removed element threw an exception after it had already been removed, the value would be lost. So you read the element first with top() or front(), then call pop().
How do I make a min-heap priority_queue in C++?
By default priority_queue is a max-heap. For a min-heap write std::priority_queue<int, std::vector<int>, std::greater<int>> pq; so top() returns the smallest element.
Which container does std::stack use internally?
By default both stack and queue use std::deque, and priority_queue uses std::vector. You can choose another underlying container, for example std::stack<int, std::vector<int>>.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicset and map in STL Your progress: 56 / 81 (69%) · Lesson 57 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free