Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ vector::empty function is used to check whether the vector is empty or not. It returns true if the size of the vector 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 vector is zero, else returns false.

Time Complexity

Constant i.e, Θ(1).

Example:

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

#include <iostream>
#include <vector>
using namespace std;
 
int main (){
  vector<int> MyVector;

  cout<<"Is the Vector empty?: "<<MyVector.empty()<<"\n\n";

  cout<<"Three new elements are added in the Vector.\n";
  MyVector.push_back(10);
  MyVector.push_back(20);
  MyVector.push_back(30);

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

The output of the above code will be:

Is the Vector empty?: 1

Three new elements are added in the Vector.
Now, Is the Vector empty?: 0

❮ C++ - Vector