VOOZH about

URL: https://www.geeksforgeeks.org/cpp/how-to-find-the-size-of-a-stack-in-cpp/

⇱ How to Find the Size of a Stack in C++? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Find the Size of a Stack in C++?

Last Updated : 23 Jul, 2025

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 thestd::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 Stack

The below example demonstrates how we can use the std::size() function to find the size of a stack in C++.


Output
Size of the stack is: 4

Time Complexity: O(1)
Auxiliary Space: O(1)



Comment
Article Tags: