The empty() Method is used to check if a List is empty or not.
Let us say, we have a List that contains three names, Mohan, Kriti and Salim. And we want to check if the List is empty or not.
We can use the front() Method to achieve the above.
#include <iostream> #include <list> using namespace std; int main() { list<string> x = {"Mohan", "Kriti", "Salim"}; if (x.empty()) { cout << "The List is empty"; } else { cout << "The List is not empty"; } return 0; }
So, in the above code we have created a List and initialised to the variable x.
list<string> x = {"Mohan", "Kriti", "Salim"};
Below is how the values are positioned in the List,
Next, we have used the empty() method to check if the List is empty or not.
if (x.empty())
In this case the List is not empty.
if (x.empty()) { cout << "The List is empty"; } else { cout << "The List is not empty"; }
So, we got the output as,