stack::size() 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::size() is a C++ Standard Library function which is used to get the number of elements in the stack.

Syntax:

Stack_Name.size( );

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

Return value: The stack::size() function returns the number of elements in the stack container.

Example of stack::size() Function

Program

#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 Reply

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

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