Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays.
empty() function is used to check if the array container is empty or not.
Syntax :
arrayname.empty() Parameters : No parameters are passed. Returns : True, if array is empty False, Otherwise
Examples:
Input : myarray{1, 2, 3, 4, 5};
myarray.empty();
Output : False
Input : myarray{};
myarray.empty();
Output : True
Errors and Exceptions
1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.
// Non Empty array example // CPP program to illustrate // Implementation of empty() function #include <array> #include <iostream> using namespace std; int main() { array<int, 5> myarray{ 1, 2, 3, 4 }; if (myarray.empty()) { cout << "True"; } else { cout << "False"; } return 0; } |
Output :
False
// Empty array example // CPP program to illustrate // Implementation of empty() function #include <array> #include <iostream> using namespace std; int main() { array<int, 0> myarray; if (myarray.empty()) { cout << "True"; } else { cout << "False"; } return 0; } |
Output :
True
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to iterate over the elements of an std::tuple in C++
- How to access private/protected method outside a class in C++
- lvalues references and rvalues references in C++ with Examples
- Implementation of lower_bound() and upper_bound() in Array of Pairs in C++
- Difference between cout and std::cout in C++
- Sorting Vector of Arrays in C++
- C++ program to check whether a String is a Pangram or not
- Difference between pair in Multiset and Multimap in C++ STL
- How to use unordered_map efficiently in C++
- Difference between Array and Map
- Header files in C/C++ with Examples
- C++ Program for the Fractional Knapsack Problem
- Trigraphs in C++ with Examples
- Difference Between DART and C++
- How to fix auto keyword error in Dev-C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

