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
Call
What 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
Parameter
Description
None
top() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
T& / const T&
A reference to the newest element on top. Non-const stacks return a mutable reference.
Undefined behavior
If 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.
The stack holds 1, 2, 3 with 3 on top. The first line prints that top value.
st.top() = 99 assigns through the reference. The top element becomes 99 in place.
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;
}
Output
No pages yet, so top() is not called.
Current page: api
How It Works
The stack starts empty, so empty() is true and the program skips top(). This avoids undefined behavior.
Three page names are pushed. The newest name, "api", becomes the top.
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.