Posting useful tips and guides for programming.

Follow

How to Check if a Vector is Empty in C++?

Are you struggling with how to check if vector is empty in CPP (C++) đ¤?

In C++ vector is similar to dynamic arrays, they can automatically adjust their size when an element is added or removed, and the container manages their storage for them like if we add or delete any element from the vector its size will change accordingly.Â

In this article, weâll discuss What is a vector in CPP, and how to check if the vector is empty in CPP. So without further ado, letâs dive deep into the topic. Letâs go over a few examples.

Â

What is a Vector in C++?

As weâve discussed in C++, vectors are dynamic arrays so before inserting or deleting elements in a vector, itâs important to first know about the size of the vector. If a vector is empty it means its size is zero. And to check if a vector is empty or not weâll use the empty() function in C++.

Â

Code

```#include<iostream>

#include<vector>

void print(std::vector <int> const &V) {

Â Â Â std::cout << "Elements of vector: ";

Â Â Â for(int i=0; i < V.size(); i++)

Â Â Â std::cout << V.at(i) << ' ';

}

int main() {

Â Â Â std::vector<int> V = {2,5,7,8};

Â Â Â print(V);

Â Â Â return 0;

}```

Â

Output

`Elements of vector: 2 5 7 8`

Â

Â

How to Check if a Vector is Empty in C++?

In C++, there are two solutions to Â check if a vector is empty or not, they are the following;Â

1. Use empty() to check the size of the vector
2. Use size() to check the size of the vector

Â

Method 1: Use empty() to Check The Size of The Vector

To determine whether or not the vector container is empty, use the empty() function.

Â

Code

```#include <iostream>

#include <vector>

using namespace std;

Â

int main()

{

Â Â Â Â vector<int> My_vector{};

Â Â Â Â if (My_vector.empty())

Â Â Â Â {

Â Â Â Â Â Â Â Â cout << "True:Â  ";

Â Â Â Â Â Â Â Â cout << "Vector is Empty";

Â Â Â Â }

Â Â Â Â else {

Â Â Â Â Â Â Â Â cout << "False Â  : Â  ";

Â Â Â Â Â Â Â Â cout << "Vector isÂ  not Empty";

Â Â Â Â Â Â Â Â

Â Â Â Â }

Â Â Â Â return 0;

}```

Â

Output

`True:Â  Vector is Empty`

Â

You can see in the above example that the program displays True: Vector is Empty, which means the vector we have declared is empty.

Â

Code

```#include <iostream>

#include <vector>

using namespace std;

Â

int main()

{

Â Â Â Â vector<int> My_vector{1, 2};

Â Â Â Â if (My_vector.empty())

Â Â Â Â {

Â Â Â Â Â Â Â Â cout << "True:Â  ";

Â Â Â Â Â Â Â Â cout << "Vector is Empty";

Â Â Â Â }

Â Â Â Â else {

Â Â Â Â Â Â Â Â cout << "False: Â  ";

Â Â Â Â Â Â Â Â cout << "Vector isÂ  not Empty";Â  Â  Â

Â Â Â Â }

Â Â Â Â return 0;

}```

Â

Output

`False: Â  Vector is not Empty`

Â

As we have seen in the above example, the else statement is executed because the vector is not empty. Vector has two elements {1,2} so if statement is not executed, print False: vector is not empty.

Â

Â

Method 2: Use size() toÂ  Check The Size of The Vector

The size() function can be used to return either the vector containerâs size or its element count.

Â

Code

```#include <iostream>

#include <vector>

using namespace std;

Â

int main()

{

Â Â Â Â vector<int> My_vector{ };

Â Â Â Â cout << My_vector.size();

Â Â Â Â return 0;

}```

Â

Â Output

`0`

Â

In the above Example Output is zero because we have declared an empty vector {}. So the size of the vector is 0.

Â

Code

```#include <iostream>

#include <vector>

using namespace std;

Â

int main()

{

Â Â Â Â vector<int> My_vector{9 ,6 ,3};

Â Â Â Â cout << My_vector.size();

Â Â Â Â return 0;

}```

Â

Output

`3`

Â

In the above example, we have declared 3 elements {9, 6, 3} in the vector so it is displaying 3 as the size of the vector is 3.

Letâs see an application of vector in Cpp to check if the vector is empty, and if it isnât, add the back element to a variable with a 0 initial value before popping it. Continue doing this until the vector is clear. Display the variableâs final value.

Â

Code

```#include <iostream>

#include <vector>

using namespace std;

Â

int main()

{

Â Â Â Â int sum = 0;

Â Â Â Â vector<int> My_vector{ 3, 5, 7, 9 };

Â Â Â Â while (!My_vector.empty())

Â Â Â Â {

Â Â Â Â Â Â Â Â sum = sum + My_vector.back();

Â Â Â Â Â Â Â Â My_vector.pop_back();

Â Â Â Â }

Â Â Â Â cout << "Total Sum: " << sum << '\n';

Â Â Â Â return 0;

}```

Â

Output

`Total Sum: 24`

Â

In the above example, we are removing elements from the vector one by one until the vector is empty, then summing up all the elements to get a total of â24.â We give { 3, 5, 7, 9 } as input and the received output is 3+5+7+9 = 24

Â

Â

Conclusion

To summarize the article: what is an empty vector in C++, to check empty vectors in C++, and how to check empty vectors. We have discussed the vectors that are dynamic arrays and why checking that the vectors are empty before the insertion and deletion of any element.

Â Â Â Â Â Letâs have a quick recap of the topics discussed in this article.

1. What does c++ vector empty means?
2. How to check if a vector is empty in c++?
3. Use empty() to check the size of the vector
4. Use size() to check the size of the vector

If youâve found this article helpful, donât forget to share and comment below đ which solutions have helped you solve the problem.

What is a Virtual Function Template Class and How to Use in C++?

Do you want to learn what is a virtual function template class and how it can be implementedâŚ

How to Fix an âUndeclared Identifierâ Error in C++?

Are you worried đ about getting string undeclared identifier errors in C++ (CPP) because the identifier string isâŚ