C++ Standard Library C++ STL Library

C++ <deque> - empty() Function



The C++ deque::empty function is used to check whether the deque is empty or not. It returns true if the size of the deque is zero, else returns false.

Syntax

bool empty() const;
bool empty() const noexcept;

Parameters

No parameter is required.

Return Value

true if the size of the deque is zero, else returns false.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the deque::empty function is used to check whether the deque is empty or not.

#include <iostream>
#include <deque>
using namespace std;
 
int main (){
  deque<int> MyDeque;

  cout<<boolalpha;

  cout<<"Is the Deque empty?: "<<MyDeque.empty()<<"\n";

  cout<<"Add elements in the Deque.\n";
  MyDeque.push_back(10);
  MyDeque.push_back(20);
  MyDeque.push_back(30);

  cout<<"Now, Is the Deque empty?: "<<MyDeque.empty()<<"\n";
  return 0;
}

The output of the above code will be:

Is the Deque empty?: true
Add elements in the Deque.
Now, Is the Deque empty?: false

❮ C++ <deque> Library