A C++ array may hold dependent data types like vectors, references, and so on. The ‘array’ in C++ standard template library is mostly a class, but they’re more effective, easier to handle, and easier to configure. The ‘array’ module provides several inbuilt functions, so the integration of operations is quicker while using it instead of C-Style arrays.
To use the ‘array’ and its functionalities, the programs must integrate the ‘array’ header file. In this article, we will look at the utility method empty() of the array class that would be applied to determine whether or not the required array is blank.
Use array::empty() method to check if the array is empty:
The array::empty() is an inbuilt method in the C++ Standard template library that analyzes whether or not the defined array is blank. This technique does not change the elements of the array. Instead, it examines if an array is blank or not, that is, if maybe the array’s size is zero. If the size of the array becomes zero, this returns 1 which means true. Otherwise, this returns 0 which means false. In this case, we apply the if-else condition along with the empty() function.
#include <array>
using namespace std;
int main() {
array<int,4> array1{5, 10, 15,};
array<int,0> array2{};
array<int,0> array3{};
array<int,6> array4{88, 23, 30, 9, 47, 65};
cout<<"array1.empty(): "<<array1.empty()<<endl;
cout<<"array2.empty(): "<<array2.empty()<<endl;
cout<<"array3.empty(): "<<array3.empty()<<endl;
cout<<"array4.empty(): "<<array4.empty()<<endl;
if(array1.empty())
cout<<"array1 is empty"<<endl;
else
cout<<"array1 is not empty"<<endl;
if(array2.empty())
cout<<"array2 is empty"<<endl;
else
cout<<"array2 is not empty"<<endl;
if(array3.empty())
cout<<"array3 is empty"<<endl;
else
cout<<"array3 is not empty"<<endl;
if(array4.empty())
cout<<"array4 is empty"<<endl;
else
cout<<"array4 is not empty"<<endl;
return 0;
}
Here, we are going to integrate the header files <iostream> and <array>. <iostream> header file that includes object declarations such as cout, cin, and many more. header file deals with the fixed-length arrays in the program. Along with this, we have utilized a standard namespace.
Now, we call the main() function. Here, we declare four different arrays. We specify the size of these arrays and then set the elements of the arrays. The first array named ‘array1’ contains three elements. The second array named ‘array2’ has no element. The third array termed ‘array3’ is also empty. The last array holds 5 random values. To print these four arrays, we have been using the ‘cout’ statement. The function empty() has been invoked for these arrays separately. We check and print the message now.
If the condition is fulfilled then the ‘cout’ statement prints that the defined array is empty. Otherwise, the ‘cout’ statement prints that the array is not empty. The ‘endl’ command is used to move the cursor to the next line of the code. In the end, we have entered ‘return 0’ to terminate the program.
Use the empty() function to check if the array is blank:
Arrays in C++ are much more effective, more translucent, and more dependable than C-style arrays in general. The empty() method is used to determine whether or not the array is empty. This function accepts no arguments. If the array is blank, the function will provide True; else, this will return False. It ensures that no exceptions would be generated.
Whenever an argument is provided, an error is displayed. In this program, if the array’s size is 0, it will be considered a blank array, so the function returns ‘True’ as output.
#include <array>
using namespace std;
int main()
{
array arr;
if (arr.empty()) {
cout << "True";
}
else {
cout << "False";
}
return 0;
}
At the start of the program, two libraries <array> and <iostream> are included. Before calling the main() function the standard namespace is being used. Within the body of the main() function, we first declare the array having an integer data type. We define the condition here. The zero sizes of the array ‘arr’ shows that that array has no element. We apply the if-else condition and the empty() method is also being called.
If the required array is empty, the ‘cout’ statement prints ‘True’ else ‘cout’ statement prints ‘False’. To end the code we have integrated the ‘retrun 0’ command.
Use if-else Condition:
We are going to use the if-else condition to check whether the specified array is empty or not. Here the size of array ‘arr’ is 10 and the ‘cout’ statement will return ‘arr is not empty’.
#include <array>
using namespace std;
int main(void)
{
array<int, 10> arr;
if (arr.empty())
cout << "arr is empty" << endl;
else
cout << "arr is not empty" << endl;
}
First, two header files <array> and <iostream> have been introduced. We’ve also implemented a standard namespace. The main() function was called. The array would be first declared with an integer data type. This is where we specify the size. There are 9 elements in this array. Then the if-else condition is applied.
In addition, we utilize the empty() function. If the defined array is blank, the ‘cout’ command displays ‘True,’ or else it shows ‘False.’ We have just used the ‘endl’ command to shift the cursor to the next line in the program.
Conclusion:
We have elaborated how to utilize the empty() function to verify if an array is empty in this article. Three illustrations have been used to demonstrate this. The examples are well-explained and executed so well. Loop through the items and match these to the null character (/0) to determine if a defined array is blank or not. We may utilize array[]={} to define a blank array. Then, specify the size of the array to determine whether or not the array is blank. If an array is defined but still not filled, the indexing or number of items it can hold must be provided.