Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Containers

C++ std::stack class

Reference for std::stack: the last-in first-out adapter with push, pop, top, emplace, swap, its underlying container and the classic pop-returns-void rule.

std::stack<T> is a container adapter that gives a last-in, first-out (LIFO) interface: you push onto the top, look at the top, and pop from the top. It does not store anything itself; it wraps another container (by default std::deque<T>) and exposes only the few operations a stack needs. That narrow interface is the point: code that uses a stack tells the reader exactly how data flows.

Typical uses are undo history, parsing and expression evaluation, depth-first search, and converting recursion into a loop. You cannot iterate over a stack or look at any element except the top.

C++
#include <stack>
std::stack<int> s;
s.push(1);  s.push(2);
int top = s.top();   // 2
s.pop();             // removes 2, returns nothing

Try it

C++ Push, top and pop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Balanced brackets and a vector-backed stack

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template <class T, class Container = std::deque<T>>
class stack;

void push(const T& v);    void push(T&& v);
template <class... A> decltype(auto) emplace(A&&... args);
void pop();
T& top();                 const T& top() const;
bool empty() const;       size_type size() const;
void swap(stack& other) noexcept;
Name Description
T Element type.
Container The underlying sequence; needs back(), push_back(), pop_back(). deque, vector and list work.
v Value to push.

Returns: top() returns a reference to the newest element. pop() returns nothing (void). emplace returns a reference to the new element since C++17.

Errors / undefined behavior: calling top() or pop() on an empty stack is undefined; check empty() first.

Usage notes

  • pop() returns void. This is deliberate (exception safety: returning by value could lose the element if copying throws). Read with top(), then pop().
  • top() returns a reference that dangles after pop(). Copy the value first if you need it afterward.
  • Complexity: push, pop, top, empty and size are O(1) (amortized for push with vector).
  • No iteration, no clear(). To empty a stack, assign a new one (s = std::stack<int>();) or pop in a loop.
  • Choosing the container: the default deque never moves elements on growth; vector is more compact. Pass it as the second template argument.
  • Comparison: ==, < and friends compare the underlying containers lexicographically.
  • Constructing from a range is C++23: std::stack<int> s(std::from_range, v). In older code, move an existing container in.
  • Alternative: a std::vector with push_back / back / pop_back is also a perfectly good stack and can be iterated.