C++ List empty() functionLast Updated : 17 Mar 2025 In the C++ programming language, the list empty() function is a member function of the list container, which is part of the Standard Template Library (STL). This function's purpose is to check whether a list contains any elements or not. It returns a Boolean value if the list is empty; otherwise, it returns false. It does not modify the content of the list. ![]() Syntax:It has the following syntax: In this syntax,
Time Complexity: It has a constant time O(1) because it only checks the size of the list. C++ Simple List empty() function ExampleLet us take an example to illustrate the list empty() function in C++. ExampleCompile and RunOutput: The list is empty. The list is not empty. Explanation: In this example, we have created a list std::list<int> named numbers and initially checked using the empty() function to see if it contains any elements. As the list is empty at first, it displays the statement as "The list is empty.". After adding one element using the push_back(10) function, the list is checked again, and this time the empty() function returns false, which displays the result "The list is not empty." on the screen. C++ Example to Check If a List Is Empty Before and After Clearing using the list empty() functionLet us take an example to illustrate how to check if a list is empty or not using the list empty() function in C++. ExampleCompile and RunOutput: Before clearing: The list is not empty. After clearing: The list is empty. Explanation: In this example, we have created a list of fruits that contains three string members at the start. First, the application checks that the list is not empty with the empty() function. After that, we again use the empty() function to check that the list is now empty after using the clear() function as a method, which removes all the entries. Features of the list empty() function in C++There are several features of the list empty() function in C++. Some of them are as follows:
ConclusionIn conclusion, the C++ list::empty() is a simple and effective method to check if there are any elements in a list or not. It helps to provide safe list operations by eliminating problems like reading or removing elements from an empty list. It is efficient and dependable because it performs a continuous check without modifying the container. It checks if a list is empty before doing any operations on it, which can make the C++ code safer and easier to read. C++ list empty() function FAQ's1) What is the use of the std::list::empty() function in C++? The functionality of the empty() function in the std::list container in C++ checks whether the std::list container is empty or not. It returns true if the list is indeed empty; otherwise, it returns false if it has one or more elements. 2) Does the list::empty() function have any arguments in C++? No, the empty() function does not have any argument because it only checks the length of the list container object on which it is called. 3) What is the return type of the empty() function in C++? In C++, the return type of a function is Boolean (bool) type, i.e., it returns true if empty() is called on an empty list and returns false if it has more than 1 object. 4) What is the time complexity of the list::empty() function in C++? The time complexity of the empty() function is O(1) because it does not do anything physical, but just checks the size of the list, which is 0. 5) How is the empty() function different from checking list.size()==0? Both the checks are the same, but the empty() function is more readable, efficient, and expressive as it expresses the intent of the code in that it is checking whether the list is empty or not. Next Topicsize() Function |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India
