list empty() function in C++ STL Last Updated : 29 May, 2023 Comments Improve Suggest changes Like Article Like 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. Syntaxlist_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; } OutputEmpty List Not Empty Time Complexity: O(1)Space Complexity: O(1) Note: This function works in constant time complexity. Comment More infoAdvertise with us Next Article list front() function in C++ STL B barykrg Follow Improve Article Tags : Misc C++ STL CPP-Functions cpp-list +1 More Practice Tags : CPPMiscSTL Similar Reads List in C++ STL In C++, list container implements a doubly linked list in which each element contains the address of next and previous element in the list. It stores data in non-contiguous memory, hence providing fast insertion and deletion once the position of the element is known.Example:C++#include <iostream 7 min read Different Ways to Initialize a List in C++ STL Initializing a list means assigning some initial values to the list elements. In this article, we will learn different methods to initialize the list in C++. Letâs start from the easiest method:The easiest way to initialize a list is by passing the initial values inside an initializer list to its co 3 min read Commonly Used Methodslist::begin() and list::end() in C++ STLLists 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::begin() begin() function is used to return 3 min read list::begin() and list::end() in C++ STLLists 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::begin() begin() function is used to return 3 min read list empty() function in C++ STLThe 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. Syntaxlist_name.empty() Parameters This fu 1 min read list front() function in C++ STLThe list::front() is a built-in function in C++ STL which is used to return a reference to the first element in a list container. Unlike the list::begin() function, this function returns a direct reference to the first element in the list container. Syntaxlist_name.front(); Parameters This function 1 min read list back() function in C++ STLThe list::back() function in C++ STL returns a direct reference to the last element in the list container. This function is different from the list::end() function as the end() function returns only the iterator to the last element. Syntaxlist_name.back();ParametersThis function does not accept any 1 min read list push_front() function in C++ STLThe list::push_front() is a built-in function in C++ STL which is used to insert an element at the front of a list container just before the current top element. This function also increases the size of the container by 1.Syntaxlist_name.push_front(dataType value)ParametersThis function accepts a si 2 min read list push_back() function in C++ STLThe list:push_back() function in C++ STL is used to add a new element to an existing list container. It takes the element to be added as a parameter and adds it to the list container. Syntaxlist_name.push_back(value)ParametersThis function accepts a single parameter which is a mandatory value. This 1 min read list pop_front() function in C++ STLThe list::pop_front() is a built-in function in C++ STL which is used to remove an element from the front of a list container. This function thus decreases the size of the container by 1 as it deletes the element from the front of the list. Syntaxlist_name.pop_front();ParametersThe function does not 1 min read list pop_back() function in C++ STLThe list::pop_back() is a built-in function in C++ STL which is used to remove an element from the back of a list container. That is, this function deletes the last element of a list container. This function thus decreases the size of the container by 1 as it deletes an element from the end of the l 2 min read list insert() in C++ STLThe list::insert() is used to insert the elements at any position of list. This function takes 3 elements, position, number of elements to insert and value to insert. If not mentioned, number of elements is default set to 1. Syntax: insert(pos_iter, ele_num, ele)Parameters: This function takes in th 2 min read list erase() function in C++ STLThe list::erase() is a built-in function in C++ STL which is used to delete elements from a list container. This function can be used to remove a single element or a range of elements from the specified list container. Syntax: iterator list_name.erase(iterator position)or,iterator list_name.erase(it 3 min read list remove() function in C++ STLThe list::remove() is a built-in function in C++ STL which is used to remove elements from a list container. It removes elements comparing to a value. It takes a value as the parameter and removes all the elements from the list container whose value is equal to the value passed in the parameter of t 2 min read list::clear() in C++ STLLists 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::clear()clear() function is used to remove a 1 min read Other Member Methodslist rbegin() and rend() function in C++ STLlist::rbegin() is an inbuilt function in C++ STL that returns a reverse iterator which points to the last element of the list. Syntax: list_name.rbegin()Parameter: This function does not accept any parameters. Return value: It returns a reverse iterator which points to the last element of the list. 2 min read list cbegin() and cend() function in C++ STLThe list::cbegin() is a built-in function in C++ STL which returns a constant random access iterator which points to the beginning of the list. Hence the iterator obtained can be used to iterate container but cannot be used to modify the content of the object to which it is pointing even if the obje 2 min read list crbegin() and crend() function in C++ STLThe list::crbegin() is a built-in function in c++ STL that returns a constant reverse iterator which points to the last element of the list i.e reversed beginning of container. The elements cannot be modified or changed as the iterator is a constant one. Syntax: list_name.crbegin()Parameters: The fu 3 min read list assign() function in C++ STLThe list::assign() is a built-in function in C++ STL which is used to assign values to a list. It can also be used to copy elements from one list to another. To assign elements to a list. Syntax: list_name.assign(count, value) Parameters: This function accepts two mandatory parameters as shown in th 2 min read list::remove() and list::remove_if() in C++ STLLists 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::remove() remove() function is used to remov 3 min read list reverse function in C++ STLThe list::reverse() is a built-in function in C++ STL which is used to reverse a list container. It reverses the order of elements in the list container. Syntax: list_name.reverse()Parameters: This function does not accept any parameters. Return Value: This function does not return any value. It jus 1 min read list resize() function in C++ STLThe list::resize() is a built-in function in C++ STL which is used to resize a list container. It takes a number n as parameter and resizes the list container to contain exactly n elements. If the list already has more than n elements, then the function erases the elements from the list except the f 2 min read std::list::sort in C++ STLLists 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::sort() sort() function is used to sort the 2 min read list max_size() function in C++ STLThe list::max_size() is a built-in function in C++ STL which returns the maximum number of elements a list container can hold. Syntax: list_name.max_size()Parameters: This function does not accept any parameters. Return Value: This function returns the maximum number of elements a list container can 1 min read list unique() in C++ STLlist::unique() is an inbuilt function in C++ STL which removes all duplicate consecutive elements from the list. It works only on sorted list. Syntax: list_name.unique(BinaryPredicate name) Parameters: The function accepts a single and optional parameter which is a binary predicate that returns true 2 min read list::emplace_front() and list::emplace_back() in C++ STLLists 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::emplace_front()This function is used to ins 3 min read Nested list in C++ STL list in STL is used to represent a linked list in C++. How to create a nested list. We are given n lists, we need to create a list of n lists. Examples: Input : Number of lists: 2 1st list: {1 2} 2nd list: {3 4 5 6} Output : [ [ 1 2 ] [ 3 4 5 6 ] ] Input : Number of lists: 3 1st list : {0 1} 2nd lis 2 min read List of Vectors in C++ STL In C++, the list of vector refers to the list container in which each element is a vector. In this article, we will learn about the list of vectors in C++.Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main() { list<vector<int>> l = {{1, 3}, {2 3 min read Difference Between Vector and List Vector: Vector is a type of dynamic array which has the ability to resize automatically after insertion or deletion of elements. The elements in vector are placed in contiguous storage so that they can be accessed and traversed using iterators. Element is inserted at the end of the vector. Example: 2 min read Common List ProgramsHow to get element at specific position in List in C++The list doesn't have random access operator [] to access elements by indices, because std::list internally store elements in a doubly-linked list. So, to access an element at any Kth location, the idea is to iterate one by one from beginning to Kth element. Instead of iterating for K times. For thi 2 min read How to insert elements in C++ STL List ?List has been discussed in many articles, but the sole purpose of this article is to cover all types of insertions that are possible to be carried in a list container and to give a detailed insight on the insertion operations. List and its many functions are defined under the header file "list" . Va 6 min read C++ Remove Elements From a List While IteratingPrerequisite: List in C++ Removing elements is a necessary task for any container. Removing elements from a list is an easy task as we have predefined pop_back and pop_front functions but as the list is a container following the properties of a doubly linked list there should be a method to remove e 5 min read How to delete a range of values from the List using IteratorGiven a List, the task is to delete a range of values from this List using Iterator. Example: Input: list = [10 20 30 40 50 60 70 80 90], start_iterator = 3, end_iterator = 8 Output: 10 20 80 90 Input: list = [1 2 3 4 5] start_iterator = 1, end_iterator = 3 Output: 3 4 5 Approach: In this method, a 2 min read Like