The list::empty() is a built-in function in C++ STL that is used to check whether a particular list container is empty or not. This function does not modify the list, it simply checks whether a list is empty or not, i.e. the size of the list is zero or not.
Syntax
list_name.empty()
Parameters
- This function does not accept any parameter, it simply checks whether a list container is empty or not.
Return Value
- The return type of this function is boolean.
- It returns True is the size of the list container is zero otherwise it returns False.
Example
The below program illustrates the list::empty() function.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
list< int > demoList;
if (demoList.empty())
cout << "Empty List\n" ;
else
cout << "Not Empty\n" ;
demoList.push_back(10);
demoList.push_back(20);
demoList.push_back(30);
demoList.push_back(40);
if (demoList.empty())
cout << "Empty List\n" ;
else
cout << "Not Empty\n" ;
return 0;
}
|
OutputEmpty List
Not Empty
Time Complexity: O(1)
Space Complexity: O(1)
Note: This function works in constant time complexity.