C++ Stack Library - empty() Function



Description

The C++ function std::stack::empty() tests whether stack is empty or not. Stack of zero size is considered as empty stack.

Declaration

Following is the declaration for std::stack::empty() function form std::stack header.

C++98

bool empty() const;

Parameters

None

Return value

Returns true if stack is empty otherwise false.

Exceptions

Provides no-throw guarantee for standard container.

Time complexity

Constant i.e. O(1)

Example

The following example shows the usage of std::stack::empty() function.

#include <iostream>
#include <stack>

using namespace std;

int main(void) {
   stack<int> s;

   if (s.empty())
      cout << "Stack is empty." << endl;

   s.emplace(1);

   if (!s.empty())
      cout << "Stack is not empty." << endl;

   return 0;
}

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

Stack is empty.
Stack is not empty.
stack.htm
Advertisements