In C++, you can check if an array is empty by comparing its size with 0.
In this example,
numbers
with some elements.isEmpty
.#include <iostream>
int main() {
int numbers[] = {1, 2, 3};
int size = sizeof(numbers) / sizeof(numbers[0]);
bool isEmpty = size == 0;
std::cout << "Array is empty: " << (isEmpty ? "true" : "false") << std::endl;
return 0;
}
Array is empty: false
In this example,
emptyArray
.isEmpty
.#include <iostream>
int main() {
int emptyArray[0];
int size = sizeof(emptyArray) / sizeof(emptyArray[0]);
bool isEmpty = size == 0;
std::cout << "Array is empty: " << (isEmpty ? "true" : "false") << std::endl;
return 0;
}
Array is empty: true
In this tutorial, we learned How to check if an Array is Empty in C++ language with well detailed examples.