.top()

Published Jul 29, 2022Updated Dec 21, 2022
Contribute to Docs

The .top() method returns the top element on the stack. This will be the most recently added element on the stack since they follow a last-in first-out (LIFO) insertion order.

Syntax

The .top() method can be called on a stack using the following syntax:

stackName.top();

Codebyte Example

The following example calls the .top() method to print successive entries from myStack:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn C++ on Codecademy