In this article, we will discuss different ways to check if a vector is empty or not in C++.

**Table Of Contents**

## Method 1: using vector::empty()

In C++. the vector class provides a function empty(). It returns true if the calling vector object is empty, otherwise it returns false. Let’s see an example,

**Advertisements**

#include <vector> #include <iostream> using namespace std; int main() { vector<int> vecObj; // Check if a vector is empty if ( vecObj.empty() ) { cout<< "Vector is Empty" << endl; } else { cout<< "Vector is not Empty" << endl; } return 0; }

**Output:**

Vector is Empty

We created a vector with no elements, therefore the vector::empty() function returned true.

### Frequently Asked:

## Method 2: using vector::size()

In C++, the vector class provides a function size(). It returns the number fo elements in the calling vector object. If a vector is empty then the vector::size() function will return 0, for that vector object. Let’s see an example,

#include <vector> #include <iostream> using namespace std; int main() { vector<int> vecObj; // Check if a vector is empty // by confirming if its size is 0 if ( vecObj.size() == 0) { cout<< "Vector is Empty" << endl; } else { cout<< "Vector is not Empty" << endl; } return 0; }

**Output:**

Vector is Empty

We created a vector with no elements, therefore the vector::size() function returned 0.

## Method 3: using vector::begin() & vector::end()

The vector::begin() function returns an iterator that points to the first element of vector. Whereas, the vector::end() function returns an iterator that points one past the last

element in the vector. So, if a vector is empty, then the value returned by the begin() and end() functions will be same for that vector. Let’s see an example,

### Best Resources to Learn C++:

#include <vector> #include <iostream> using namespace std; int main() { vector<int> vecObj; // Check if a vector is empty if ( vecObj.begin() == vecObj.end() ) { cout<< "Vector is Empty" << endl; } else { cout<< "Vector is not Empty" << endl; } return 0; }

**Output:**

Vector is Empty

## Summary

We learned about different ways to check if a vector is empty or not in C++.