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

In Cpp Check if Vector is Empty

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.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts
Total
0
Share