Skip to content
Related Articles

Related Articles

Improve Article
list empty() function in C++ STL
  • Last Updated : 20 Jun, 2018

The list::empty() is a built-in function in C++ STL is used to check whether a particular list container is empty or not. This function does not modifies the list, it simply checks whether a list is empty or not, i.e. the size of 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.

Below program illustrates the list::empty() function.




// CPP program to illustrate the
// list::empty() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Creating a list
    list<int> demoList;
  
    // check if list is empty
    if (demoList.empty())
        cout << "Empty List\n";
    else
        cout << "Not Empty\n";
  
    // Add elements to the List
    demoList.push_back(10);
    demoList.push_back(20);
    demoList.push_back(30);
    demoList.push_back(40);
  
    // check again if list is empty
    if (demoList.empty())
        cout << "Empty List\n";
    else
        cout << "Not Empty\n";
  
    return 0;
}
Output:
Empty List
Not Empty

Note: This function works in constant time complexity.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :