How to Find the Size of a Stack in C++? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report 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 : 4Finding 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++ // C++ Program to illustrate how to find the size of the // stack #include <iostream> #include <stack> using namespace std; int main() { // Creating a stack stack<int> myStack; // Pushing elements into the stack myStack.push(10); myStack.push(20); myStack.push(30); myStack.push(40); // Finding the size of the stack using size() function cout << "Size of the stack is: " << myStack.size() << endl; return 0; } OutputSize of the stack is: 4 Time Complexity: O(1)Auxiliary Space: O(1) Create Quiz Comment G gauravgandal Follow 1 Improve G gauravgandal Follow 1 Improve Article Tags : C++ Programs C++ STL cpp-stack CPP Examples +1 More Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like