C++ std::stack::pop() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
C++98

Definition

In C++, pop() removes the top element of a std::stack. That is the newest element, the one top() would return. After the call, the previous element becomes the new top.

The important thing to remember is that pop() does not return the removed value. Its return type is void. If you need the value, read it with top() first, and then call pop().

This function is a member function of std::stack<T>, declared in the <stack> header. It has been available since the first C++ standard, so every compiler supports it. You call it with a dot, for example st.pop(...).

Syntax

Call pop() with empty brackets. First read the value with top() if you need it.

C++
std::stack<int> st;
st.push(5);
st.push(30);
st.push(10);

int value = st.top();   // read 10 first
st.pop();               // then remove it
// the new top is 30
CallWhat it does
st.pop()Removes the top element. Returns nothing.

To take an element out, call top() first and then pop(). A stack has no way to remove an element from the middle, only the top one.

Library declarations
C++
void pop();

// stack<T, Container> from <stack>
// It calls c.pop_back() on the underlying container.

Parameters

ParameterDescription
Nonepop() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
Nothingpop() returns void. It does not give back the removed element.
Undefined behaviorIf the stack is empty, there is nothing to remove. Check empty() first.

C++ Examples

Example 1 — Read with top(), Then pop()

Save the top value, remove it, and look at the new top.

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

int main() {
    std::stack<int> st;
    st.push(5);
    st.push(30);
    st.push(10);

    int removed = st.top();
    st.pop();

    std::cout << "Removed: " << removed << '\n';
    std::cout << "New top: " << st.top() << '\n';
    std::cout << "Size: " << st.size() << '\n';
    return 0;
}

How It Works

  1. top() reads the newest value 10 and stores it in removed. This is needed because pop() gives nothing back.
  2. pop() takes 10 out of the stack. The previous value, 30, becomes the new top.
  3. The size drops from 3 to 2. The last two lines show the new top and the new size.

Example 2 — Pop Everything to Reverse a Sequence

Keep calling top() and pop() until the stack is empty. The values come out reversed.

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

int main() {
    std::vector<int> data = {4, 1, 7, 3};
    std::stack<int> st;
    for (int n : data) {
        st.push(n);
    }

    std::cout << "Reversed:";
    while (!st.empty()) {
        std::cout << ' ' << st.top();
        st.pop();
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The loop pushes every vector value onto the stack. The last vector value, 3, becomes the top.
  2. Each round of the drain loop prints top() and then removes it with pop(), so the previous value becomes the top.
  3. This is a classic reverse trick. The loop ends when empty() is true.

Example 3 — Pop Matching Brackets

Push opening brackets. On a closing bracket, pop if the stack is not empty.

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

int main() {
    std::string text = "([])";
    std::stack<char> st;
    bool ok = true;

    for (char ch : text) {
        if (ch == '(' || ch == '[') {
            st.push(ch);
        } else if (ch == ')' || ch == ']') {
            if (st.empty()) {
                ok = false;
                break;
            }
            char open = st.top();
            st.pop();
            if ((ch == ')' && open != '(') || (ch == ']' && open != '[')) {
                ok = false;
                break;
            }
        }
    }

    if (!st.empty()) {
        ok = false;
    }
    std::cout << (ok ? "balanced\n" : "not balanced\n");
    return 0;
}

How It Works

  1. Opening brackets are pushed. Closing brackets must match the current top.
  2. Before each pop(), the code checks empty(). That avoids undefined behavior on a bad closing bracket.
  3. After the loop, leftover openings mean the text was not balanced. Here every push has a matching pop, so the result is balanced.

Frequently Asked Questions

It removes the top element (the newest one) from the stack. It does not return the removed value.
No. pop() returns void. Read the value with top() first, and then call pop(), for example int x = st.top(); st.pop();.
It is undefined behavior. The program may crash. Check that !st.empty() before you call pop().
No. You can only remove the top element with pop(). If you need random removal, use a different container such as std::vector or std::deque directly.
Both return void, but stack::pop() removes the newest element (LIFO) while queue::pop() removes the oldest element (FIFO).

Next: push()

Add one element on top of the stack with std::stack.

push() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful