In C++, the standard library gives us the facility to use the Stacks as a type of container adaptors with LIFO type of working. The stack::top() is a C++ Standard Library function which is used to get the top element of the stack.
Syntax:
Stack_Name.top( );
Parameter: The stack::top() function does not accepts any parameter.
Return value: The stack::top() function returns the current top element of the stack container.
Example of stack::top() Function
#include <iostream> #include <stack> using namespace std; int main() { stack<int> STK; STK.push(10); STK.push(20); STK.push(30); while (!STK.empty()) { cout << STK.top() << ' '; STK.pop(); } }
Output
30 20 10