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::size() is a C++ Standard Library function which is used to get the number of elements in the stack.
Syntax:
Stack_Name.size( );
Parameter: The stack::size() function does not accepts any parameter.
Return value: The stack::size() function returns the number of elements in the stack container.
Example of stack::size() Function
Program
#include <iostream> #include <stack> using namespace std; int main() { stack<int> STK; STK.push(10); STK.push(20); STK.push(30); cout << STK.size() << endl; while (!STK.empty()) { STK.pop(); cout << STK.size() << endl; } }
Output
3 2 1 0