![]() |
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++. Example:Input: myStack = {40, 30, 20, 10} Output: Size of the stack is : 4 Finding the Size of a Stack in C++To find the size of a std::stack in C++, we can use the std::stack::size() function provided by the std::stack class template that returns an integer value that denotes the stack container’s number of elements. C++ Program to Find the Size of a StackThe below example demonstrates how we can use the std::size() function to find the size of a stack in C++. C++
Output
Size of the stack is: 4 Time Complexity: O(1) |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |