Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++ Stack::empty() Function



The C++ std::stack::empty() function checks whether the stack is empty or not (having no elements). A stack of size zero is considered an empty stack.

This function does not accept any parameter, and it is a const member function. The "const" keyword indicates that the function does not modify the stack. It returns a boolean value true if the stack is empty, and false otherwise.

When a stack is empty, attempting to pop an element from it will result in an error because there are no elements to remove. Therefore, empty stacks are commonly used as a starting point for operations that involve pushing elements.

Syntax

Following is the syntax for std::stack::empty() function −

bool stack_name.empty() const;

Parameters

This method does not accept any parameter.

Return value

Returns true if the stack is empty, otherwise false.

Example 1

The following example shows the usage of the std::stack::empty() function. At first, we are trying to create a stack s with no elements in it and we verify it using the empty() function. Then, we insert/push element '1' into the stack and use the empty() function to determine if this stack is empty.

#include <iostream>
#include <stack>
using namespace std;

int main(void) {
   stack<int> s;
   if (s.empty()){
      cout << "Stack is empty." << endl;}
   else {
      cout << "Stack is not empty." << endl;} 
   s.emplace(1);
   if (!s.empty()){
      cout << "Stack is not empty." << endl;}
   else {
      cout << "Stack is empty." << endl;} 
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

Stack is empty.
Stack is not empty.

Example 2

Here, we are trying to insert elements into the stack in a sequence of numbers from 1 to 6. Then, we use the pop() function to remove the elements one by one and calculate the product of the elements until the stack becomes empty.

#include <iostream>
#include <stack>
using namespace std;

int main() {
   stack<int> s;
   // initializing the variable Product with the value 1
   int Product = 1;
   for (int i=1;i<=6;i++)
   s.push(i);
   while (!s.empty()){
      // multiplying the topmost element of the stack with Product  
      Product = Product * s.top();
      s.pop();
   }
   cout << "The Product of elements is: " << Product << '\n';
   return 0;
}

Output

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

The Product of elements is: 720

Example 3

In the following example, we create an empty stack s and then push the values '10', '20', '30', '3', and '0' into it. We then use the empty() function to check if the stack is empty or not, and print the result accordingly.

#include <iostream>
#include <stack>
using namespace std;

int main() {
   stack<int> s;
   s.push(10);
   s.push(20);
   s.push(30);
   s.push(3);
   s.push(0);
   cout<<"stack s is empty: \n"<<s.empty()<<"\n"; 
   //empty stack  
   stack<int> s1;
   cout<<"stack s1 is empty: \n"<<s1.empty(); 
}

Output

Folowing is an output of the above code −

stack s is empty: 
0
stack s1 is empty: 
1
Advertisements