The Wayback Machine - https://web.archive.org/web/20240711053202/https://www.geeksforgeeks.org/list-empty-function-in-c-stl/
Open In App

list empty() function in C++ STL

Last Updated : 29 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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

Time Complexity: O(1)
Space Complexity: O(1)

Note: This function works in constant time complexity.


Previous Article
Next Article

Similar Reads

list::empty() and list::size() in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::empty()empty() function is used to check if the list container is empty or not. Syntax : list
3 min read
multiset empty() function in C++ STL
The multiset::empty() function is a built-in function in C++ STL which checks if the multiset is empty or not. It returns true if the multiset is empty, else it returns false. Syntax: multiset_name.empty() Parameters: The function does not accept any parameter. Return Value: The function returns true if the multiset is empty, else it returns false.
1 min read
unordered_multiset empty() function in C++STL
The unordered_multiset::empty() is a built-in function in C++ STL which returns a boolean value. It returns true if the unordered_multiset container is empty. Otherwise, it returns false. Syntax: unordered_multiset_name.empty() Parameters: The function does not accepts any parameter. Return Value: It returns a boolean value which denotes whether a
2 min read
unordered_multimap empty() function in C++ STL
The unordered_multimap::empty() is a built-in function in C++ STL which returns a boolean value. It returns true if the unordered_multimap container is empty. Otherwise, it returns false. Syntax: unordered_multimap_name.empty() Parameters: The function does not accept any parameter. Return Value: It returns a boolean value which denotes whether a u
2 min read
multimap empty() function in C++ STL
The multimap::empty() is a boolean type observer function in C++ STL which tells whether the container is empty or not. This function returns true when the multimap container is empty (i.e. the size of the container is 0). Being an observer function it does not modify the multimap in any way. Syntax: multimap1.empty() Return Value: This method retu
1 min read
unordered_set empty() function in C++ STL
The unordered_set::empty is a built-in function in C++ STL which is used to check if an unordered_set container is empty or not. It returns True if the unordered_set container is empty, otherwise it returns False. Syntax: set_name.empty()Parameters: This function does not accepts any parameter. Return Value: It returns a boolean value True if the u
2 min read
queue::empty() and queue::size() in C++ STL
Queue is a type of container adaptor that operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. queue::empty() empty() function is used to check if the queue container is empty or not. SyntaxqueueName.empty()ParametersThis function does not accept any parameter.Return Val
4 min read
stack empty() and stack size() in C++ STL
Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only. stack::empty()empty() function is used to check if the stack container is empty or not. Syntax : stackname.empty()Parameters :No parameters are passed.Returns :True, if sta
3 min read
priority_queue::empty() and priority_queue::size() in C++ STL
Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. However, in C++ STL (by default) the largest element is at the top. We can also create a priority queue having the smallest element at the top by simply passing an extr
4 min read
vector::empty() and vector::size() in C++ STL
Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::empty() The empty() function is used to check if the vector container is empty or not.Syntax : vectorname.empty() Parameters : No parameters are p
5 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg