stack::empty() function in C++ STL

In C++, the standard library gives us the facility to use the Stacks as a type of container adaptors with LIFO type of working. The stack::empty() is a C++ Standard Library function which is used to check whether the stack container is empty or not and returns true for empty Stack and false for non empty Stack.

Syntax

Stack_Name.empty( );

Parameter: The stack::empty() function does not accepts any parameter.

Return value: The stack::empty() function returns the Boolean value as follows:

  • True: When stack is empty.
  • False: When stack is not empty.

Example of stack::empty() Function

#include <iostream> 
#include <stack> 
using namespace std; 
  
int main() 
{ 
    stack<int> STK;
    
    STK.push(10);
    
    STK.push(20); 
    
    STK.push(30); 
    
    cout << STK.size() << endl;
  
    while (!STK.empty()) 
    { 
        STK.pop(); 
        cout << STK.size() << endl;
    } 
} 

Output

3
2
1
0

This article is written by

Pratik Singhal

Pratik Singhal

Please comment below, if you have any doubts or find any error in the above article.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]