Updated March 29, 2023
Introduction to C++ Empty Vector
In C++, the vector has an empty() function that helps check whether the vector container has elements. Vectors are almost similar to dynamic arrays, which have the facility to resize itself automatically when an item is deleted or inserted, with its storage able to handle automatically by the container. The elements of the vector are kept in contiguous storage, which can be accessed as well as traversed with the help of iterators. Moreover, the item is inserted at the end, which takes differential time, as occasionally, there is a need for array extension. Let us look into an empty vector in detail.
Syntax
Following is the syntax of the empty vector.
v.empty()
Here, v is the name of the vector.
Parameters: No need to pass any parameters.
Return value: If the vector is empty, true will be returned. Otherwise, false will be returned.
How Empty Vector function work in C++?
Suppose there is a vector v and contains elements 56,73,58,89,10. If the v.empty() method is called, it can be seen that the output will be displayed as false.
The code for the same will be as shown below.
v={56,73,58,89,10}
v.empty() ;
Output: False
In the meantime, if another vector vtr is present and does not contain any elements. If the v.empty() method is called this time, it can be seen that the output will be displayed as true.
The code for the same will be as shown below.
v={}
v.empty() ;
Output: True
Exceptions:
Below are the exceptions of the empty() method of the vector class.
There is no guarantee for exception throw.
An error will be shown when a parameter is passed.
Examples of C++ Empty Vector
The following are the sample programs for the empty vector.
Example #1
C++ program to check the vector is empty or not.
Code:
// C++ program to implement empty() function
#include <iostream>
#include <vector>
using namespace std ;
int main()
{
//create a vector v with different integer elements
vector<int> v{ 39, 52, 62, 31, 91, 24} ;
//print the size of the vector
cout <<"The size of vector v: \n"<< v.size() <<endl ;
//checks whether the vector is empty or not
cout << "Checks whether the vector v is empty or not? : \n" << v.empty() <<endl ;
//create a vector vtr with no elements
vector<int> vtr{ } ;
//print the size of the vector
cout <<"The size of vector vtr: \n" << vtr.size() <<endl ;
//checks whether the vector is empty or not
cout << "Checks whether the vector vtr is empty or not? : \n" << vtr.empty() <<endl ;
return 0;
}
Output:
First, create a vector v with different integer elements 39, 52, 62, 31, 91, 24. After creating it, print the size of the vector. Then, it checks whether the vector is empty using the empty() method. Once this is done, create a vector vtr with no elements and print the size of the vector. Once again, check whether the vector is empty using the empty() method. On executing the code, it can be seen that the size of the vector v is 6, and the vector is not empty as 0 is printed. At the same time, the size of the vector vtr is 0, and the vector is empty as 1 gets printed. Here, 0 is the Boolean value of false, and 1 is the Boolean value of true.
Example #2
C++ program to add elements to vector and checks whether the vector is empty or not before adding those elements
Code:
#include <iostream>
#include <vector>
using namespace std ;
int main()
{
vector<int> v;
//print the size of vector v
cout << "Size of vector: " << v.size() << endl ;
//checks whether vector v is empty or not
//if it is empty
if (v.empty())
cout << "The input vector is empty." << endl ;
//if it is not empty
else
cout << "The input vector is not empty." << endl ;
//push elements to the vector
v.push_back(45) ;
v.push_back(23);
v.push_back(56);
v.push_back(25);
v.push_back(57);
//print the size of the vector
cout << "Size of vector: " << v.size() << endl;
//checks whether the vector is empty or not
//if it is empty
if (v.empty())
cout << "The input vector is empty." << endl;
//if it is not empty
else
cout << "The input vector is not empty." << endl;
return 0;
}
Output:
In this program, create a vector v and print the vector size. Then, check whether the vector is empty using the empty() method. After checking, add elements to it using the push_back() method. Once again, print the size and check whether the vector is empty using the empty() method. On executing the code, the corresponding messages get displayed.
Example #3
C++ program to find the sum of elements in the vector.
Code:
// C++ program to find sum of elements in a vector
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//declare a variable s which is equal to zero
int s = 0;
//create a vector v with different integer elements
vector<int> v{ 39, 52, 62, 31, 91, 24};
//check whether the vector is empty
while (!v.empty())
{
//find the sum of the elements
s = s + v.back();
v.pop_back();
}
//print sum of the elements
cout << s;
return 0;
}
Output:
First, declare a variable s, which is initialized to zero. Then, create a vector v with different integer elements 39, 52, 62, 31, 91, 24. After creating it, check whether the vector is empty using the empty() method. If the vector is not empty, enter the loop. The Sum of the elements will be identified using that loop. On executing the code, it can be seen that sum of the elements gets printed.
Advantages
The following are the advantages of Vector.
- The size of the vector can be changed
- Multiple objects can be stored
- Elements can be deleted from a vector
Conclusion – C++ Empty Vector
In C++, the vector has an empty() function that checks whether the vector has elements or not. This article explains different aspects such as syntax, working, advantages, and examples of empty() function in the vector in detail.
Recommended Articles
This is a guide to C++ Empty Vector. Here we discuss How Empty Vector function work in C++ and Examples along with codes and outputs. You may also have a look at the following articles to learn more –