![]() |
VOOZH | about |
In C++, the stack is a container that follows the Last In First Out (LIFO) rule. While working with a stack, the size of the stack is required in various operations. In this article, we will learn how to find the size of a stack in C++.
Input: myStack = {40, 30, 20, 10} Output: Size of the stack is : 4
To find the size of a std::stack in C++, we can use the std::stack::size() function provided by thestd::stack class template that returns an integer value that denotes the stack container's number of elements.
The below example demonstrates how we can use the std::size() function to find the size of a stack in C++.
Size of the stack is: 4
Time Complexity: O(1)
Auxiliary Space: O(1)