To check if a list is not empty in C++, you can use the empty()
method of the std::list
class.
In this example,
std
namespace.myList
with some elements.myList
is not empty by calling the empty()
method.#include <iostream>
#include <list>
int main() {
std::list<int> myList = {1, 2, 3};
if (!myList.empty()) {
std::cout << "The list is not empty" << std::endl;
} else {
std::cout << "The list is empty" << std::endl;
}
return 0;
}
The list is not empty
In this example,
std
namespace.emptyList
without any elements.emptyList
is not empty by calling the empty()
method.#include <iostream>
#include <list>
int main() {
std::list<int> emptyList;
if (!emptyList.empty()) {
std::cout << "The list is not empty" << std::endl;
} else {
std::cout << "The list is empty" << std::endl;
}
return 0;
}
The list is empty
In this tutorial, we learned How to check if a List is Not Empty in C++ language with well detailed examples.