In C++, you can use the size
function or the size()
method to check if an array is not empty.
In this example,
numbers
with some elements.size
function or the size()
method of the array and comparing it with 0, and store the result in the variable notEmpty
.#include <iostream>
#include <array>
int main() {
std::array<int, 3> numbers = {1, 2, 3};
bool notEmpty = !numbers.empty();
std::cout << "Array is not empty: " << std::boolalpha << notEmpty << std::endl;
return 0;
}
Array is not empty: true
In this example,
emptyArray
.size
function or the size()
method of the array and comparing it with 0, and store the result in the variable notEmpty
.#include <iostream>
#include <array>
int main() {
std::array<int, 0> emptyArray;
bool notEmpty = !emptyArray.empty();
std::cout << "Array is not empty: " << std::boolalpha << notEmpty << std::endl;
return 0;
}
Array is not empty: false
In this tutorial, we learned How to check if an Array is Not Empty in C++ language with well detailed examples.