stack empty() and stack size() in C++ STL Last Updated : 08 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The std::stack::size() and std::stack::empty() in C++ are built-in functions that are used to provide information about the size of the stack. They are the member functions of the std::stack container defined inside <stack> header file.stack::empty()The stack::empty() method is used to check whether the stack is empty or not. C++ // C++ program to illustrate how to use // stack::empty() function #include <bits/stdc++.h> using namespace std; int main() { stack<int> st; // Checking if the stack st is empty if (st.empty()) cout << "Stack is Empty" << endl; else cout << "Stack is NOT Empty" << endl; // Inserting an element st.push(11); // Again checking if the stack st is empty if (st.empty()) cout << "Stack is Empty" << endl; else cout << "Stack is NOT Empty" << endl; return 0; } OutputStack is Empty Stack is NOT Empty Syntaxst.empty();ParametersThis function does not take any parameter.Return ValueReturns true if the stack is empty.Returns false if the stack is not empty.stack::size()The stack::size() method is used to find the number of elements in the stack container. C++ // C++ program to illustrate how to use stack::size() #include <bits/stdc++.h> using namespace std; int main() { stack<int> st; st.push(11); st.push(13); st.push(9); // Finding the size of the stack st int n = st.size(); cout << "Size : " << n << endl; return 0; } OutputSize : 3 Syntaxst.size();ParametersThis function does not take any parameters.Return ValueReturns the number of element present in the stack container.If there are no elements in the stack, returns 0.Difference Between stack::size() and stack::empty()Both the stack::size() and stack::empty() methods are give the information about the size of stack, but there are some differences between them which are listed below:stack::empty()stack::size()It is used to return whether the stack is empty or not.It is used to return the number of elements in the stack.Its syntax is:-stack_name.empty();Its syntax is:-stack_name.size();Its return type is of boolean.Its return type is of integer. Comment A AyushSaxena Follow Improve A AyushSaxena Follow Improve Article Tags : Misc C++ STL CPP-Library CPP-Functions cpp-containers-library cpp-stack cpp-stack-functions +4 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++5 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 through C++ Classes8 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++4 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL3 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like