stack empty() and stack size() in C++ STL


In this article we will be discussing the working, syntax and examples of stack::empty() and stack::size() function in C++ STL.

What is Stack in C++ STL?

Stacks are the data structure that stores the data in LIFO (Last In First Out) where we do insertion and deletion from the top of the last element inserted. Like a stack of plates, if we want to push a new plate into the stack we insert on the top and if we want to remove the plate from the stack we then also remove it from the top.

What is stack::empty()?

stack::empty() function is an inbuilt function in C++ STL, which is defined in <stack>header file. empty() is used to check whether the associated container is empty or not and return true or false accordingly.

The function checks the container should be empty means the size of the container should be 0.

Syntax

stack_name.empty();

Parameters

The function accepts no parameter(s).

Return value

This function returns true if the container is empty, else false.

Input 

std::stack<int> stack1;
stack1.emplace(1);
stack1.emplace(2);
stack1.emplace(3);
stack1.empty();

Output 

false

Input 

std::stack<int> stack2;
stack2.empty();

Output 

true

Example

 Live Demo

#include <iostream>
#include <stack>
using namespace std;
int main(){
   stack<int> stck;
   int Product = 1;
   stck.push(1);
   stck.push(2);
   stck.push(3);
   stck.push(4);
   stck.push(5);
   stck.push(6);
   while (!stck.empty()){
      Product = Product * stck.top();
      stck.pop();
   }
   cout<<"\nProduct of elements in stack are: "<<Product;
   return 0;
}

Output

If we run the above code it will generate the following output −

Product of elements in stack are: 720

What is stack::size()?

stack::size() function is an inbuilt function in C++ STL, which is defined in <stack>header file. size() is used to check the associated container’s size and return the result in an integer value, which is the number of elements in the container.

If the container is empty the size() returns 0

Syntax

stack_name.size();

Parameters

The function accepts no parameter(s).

Return value

This function returns the size of the container

Input 

std::stack<int> stack1;
stack1.emplace(1);
stack1.emplace(2);
stack1.emplace(3);
stack1.size();

Output 

3

Input

std::stack<int> stack2;
stack2.size();

Output 

0

Example

 Live Demo

#include <iostream>
#include <stack>
using namespace std;
int main(){
   stack<int> stck;
   int Product = 1;
   stck.push(1);
   stck.push(2);
   stck.push(3);
   stck.push(4);
   stck.push(5);
   stck.push(6);
   cout<<"size of stack is: "<<stck.size();
   while (stck.size()>0){
      Product = Product * stck.top();
      stck.pop();
   }
   cout<<"\nProduct of elements in stack are: "<<Product;
   return 0;
}

Output

If we run the above code it will generate the following output −

size of stack is: 6
Product of elements in stack are: 720

Updated on: 22-Apr-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements