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

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

Definition

In C++, top() is the only way to look at an element of a std::stack. It returns the newest element, the one that was pushed last, because a stack is last in, first out (LIFO).

top() only peeks. It does not remove the element, so calling it twice gives the same answer until you pop(). A stack has no [] operator and no iterators, so top() is the one element you can see. On a non-const stack the result is a reference, so you can also assign through it.

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

Syntax

Call top() on the stack. It takes no arguments.

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

int newest = st.top();   // 10, the last value pushed
st.top() = 99;           // assign through the reference
CallWhat it does
st.top()Returns a reference to the top element. The element stays in the stack. On a non-const stack you can assign through it.

Unlike priority_queue::top(), a non-const stack::top() is not const, so st.top() = value is allowed. Changing the top does not reorder anything, because a stack does not sort. A const stack gives a const reference, so assignment is blocked.

Library declarations
C++
reference       top();
const_reference top() const;

// stack<T, Container> from <stack>
// top() returns c.back(), the last element
// of the underlying container (usually deque).

Parameters

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

Return Value

Return ValueDescription
T& / const T&A reference to the newest element on top. Non-const stacks return a mutable reference.
Undefined behaviorIf the stack is empty there is no element to return. Check empty() first.

C++ Examples

Example 1 — Read the Top Element

Push four numbers. The last one pushed is always on top, and reading it does not remove it.

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

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

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

How It Works

  1. Four values are pushed in order. A stack is LIFO, so the last push, 20, sits on top.
  2. top() only reads the top. The second call still gives 20, because nothing was removed.
  3. size() still returns 4. This proves top() never changes the stack. Only pop() can remove an element.

Example 2 — Change the Top with Assignment

Because top() returns a reference, you can write a new value into the top element without popping.

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

int main() {
    std::stack<int> st;
    st.push(1);
    st.push(2);
    st.push(3);

    std::cout << "Before: " << st.top() << '\n';
    st.top() = 99;
    std::cout << "After:  " << st.top() << '\n';
    std::cout << "Size:   " << st.size() << '\n';
    return 0;
}

How It Works

  1. The stack holds 1, 2, 3 with 3 on top. The first line prints that top value.
  2. st.top() = 99 assigns through the reference. The top element becomes 99 in place.
  3. The size stays 3. Assignment edits the top; it does not add or remove an element. A priority_queue would not allow this, because its top() is const.

Example 3 — Check empty() Before top()

Never call top() on an empty stack. Guard with empty() first.

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

int main() {
    std::stack<std::string> pages;

    if (pages.empty()) {
        std::cout << "No pages yet, so top() is not called.\n";
    }

    pages.push("home");
    pages.push("docs");
    pages.push("api");

    if (!pages.empty()) {
        std::cout << "Current page: " << pages.top() << '\n';
    }
    return 0;
}

How It Works

  1. The stack starts empty, so empty() is true and the program skips top(). This avoids undefined behavior.
  2. Three page names are pushed. The newest name, "api", becomes the top.
  3. The second check !pages.empty() is true. Now top() is safe and prints the current page.

Frequently Asked Questions

It returns a reference to the newest element on top of the stack without removing it. On a non-const stack you can also assign through that reference.
No. top() only peeks. Call pop() afterwards to remove it. The usual pattern is to read with top() and then call pop().
It is undefined behavior. The program may crash or return garbage. Always check that !st.empty() before calling top().
Yes, on a non-const stack. Write st.top() = newValue. A const stack returns a const reference, so assignment is not allowed.
Both peek at the top, but stack::top() on a non-const stack returns a mutable reference, while priority_queue::top() is always const so you cannot change the heap order by assignment.

Back to the function index

See all 8 std::stack functions grouped by what they do.

Function index →

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.

14 people found this page helpful