Skip to content
Related Articles

Related Articles

Improve Article

array::empty() in C++ STL

  • Last Updated : 26 Mar, 2018


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.

array::empty()

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
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :