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

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

Definition

In C++, size() returns the number of elements currently stored in a std::stack. A new stack has size 0. Every push() adds one, and every pop() takes one away.

Duplicates count too. If you push the same number three times, the size is 3. The size says nothing about order beyond the count. You still use top() to see the newest element.

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.size(...).

Syntax

Call size() on the stack. It takes no arguments and returns an unsigned number.

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

std::size_t n = st.size();   // 2
CallWhat it does
st.size()Returns how many elements the stack holds. The stack is not changed.

The return type is size_type, an unsigned integer. Be careful when you subtract from it. If the stack is empty, st.size() - 1 does not give -1. It wraps around to a very large number. Check empty() first.

Library declarations
C++
size_type size() const;

// stack<T, Container> from <stack>
// size() returns c.size()

Parameters

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

Return Value

Return ValueDescription
size_typeThe number of elements in the stack, as an unsigned integer. It is 0 for an empty stack.

C++ Examples

Example 1 — Watch the Size Change

Print the size after every change to see how push() and pop() move it up and down.

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

int main() {
    std::stack<int> st;
    std::cout << "Start: " << st.size() << '\n';

    st.push(10);
    st.push(40);
    st.push(20);
    std::cout << "After 3 pushes: " << st.size() << '\n';

    st.pop();
    std::cout << "After 1 pop: " << st.size() << '\n';
    return 0;
}

How It Works

  1. A new stack has no elements, so size() prints 0.
  2. Each push() adds one element. After three pushes the size is 3.
  3. pop() removes the top element (20), so the size falls to 2. The value that was removed is not printed.

Example 2 — Duplicates Count, top() Does Not Change the Size

The stack keeps every pushed element, even equal ones. Reading with top() never removes anything.

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

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

    std::cout << "Size: " << st.size() << '\n';
    std::cout << "Top: " << st.top() << '\n';
    std::cout << "Size after top(): " << st.size() << '\n';
    return 0;
}

How It Works

  1. The same value 5 is pushed three times. A stack is not a set, so all three copies stay inside and size() is 3.
  2. top() returns one 5. It only reads the top element.
  3. The second size() is still 3, which shows that reading with top() does not shrink the stack.

Example 3 — Track Bracket Depth with size()

Push on an opening bracket and pop on a closing one. The size is the current nesting depth.

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

int main() {
    std::string text = "(a(b)c)";
    std::stack<char> depth;
    std::size_t maxDepth = 0;

    for (char ch : text) {
        if (ch == '(') {
            depth.push(ch);
            if (depth.size() > maxDepth) {
                maxDepth = depth.size();
            }
        } else if (ch == ')' && !depth.empty()) {
            depth.pop();
        }
    }

    std::cout << "Max depth: " << maxDepth << '\n';
    std::cout << "Left open: " << depth.size() << '\n';
    return 0;
}

How It Works

  1. Each '(' is pushed. After the second open parenthesis the size becomes 2, which is recorded as the max depth.
  2. Each ')' pops one opening mark when the stack is not empty. This keeps the depth correct.
  3. When the loop ends the stack is empty, so no brackets were left open. size() tracked both the peak depth and the leftover count.

Frequently Asked Questions

It returns the number of elements currently in the stack, as an unsigned size_type. An empty stack has size 0.
Yes. A stack keeps every element you push, including equal values. Pushing 5 three times gives a size of 3.
No. Both only read. Only push(), emplace(), pop(), and swap() change the contents of the stack.
Both work, but empty() is clearer and just as fast. Use size() when you need the actual count, for example to track nesting depth.
size() returns an unsigned type. On an empty stack, 0 - 1 wraps around to a very large number instead of -1. Check empty() first, or compare without subtracting.

Next: swap()

Swap the contents of two stacks in one step with std::stack.

swap() 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.

12 people found this page helpful