The Wayback Machine - https://web.archive.org/web/20240711060943/https://www.geeksforgeeks.org/listclear-c-stl/
Open In App

list::clear() in C++ STL

Last Updated : 27 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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::clear()

clear() function is used to remove all the elements of the list container, thus making it size 0.

Syntax :

listname.clear()
Parameters :
No parameters are passed.
Result :
All the elements of the list are
removed ( or destroyed )

Examples:

Input  : list{1, 2, 3, 4, 5};
list.clear();
Output : list{}

Input : list{};
list.clear();
Output : list{}

Errors and Exceptions

1. It has a no exception throw guarantee.

2. Shows error when a parameter is passed. 

CPP




// CPP program to illustrate
// Implementation of clear() function
#include <iostream>
#include <list>
using namespace std;
 
int main()
{
 list<int> mylist{ 1, 2, 3, 4, 5 };
 
 mylist.clear();
 // List becomes empty
 
 // Printing the list
 for (auto it = mylist.begin(); it != mylist.end(); ++it)
  cout << ' ' << *it;
 return 0;
}


Output:

No Output

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

Related Article : Delete elements in C++ STL List


Previous Article
Next Article

Similar Reads

map::clear() in C++ STL
Map is dictionary like data structure. It is an associative array of (key, value) pair, where only single value is associated with each unique key. map::clear() clear() function is used to remove all the elements from the map container and thus leaving it's size 0. Syntax: map1.clear() where map1 is the name of the map. Parameters: No parameters ar
2 min read
deque::clear() and deque::erase() in C++ STL
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::clear() The clear()
5 min read
forward_list::clear() and forward_list::erase_after() in C++ STL
Forward list in STL implements singly linked list. Introduced from C++11, forward list are useful than other containers in insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from list by the fact that forward list keeps track of location of only next element while list keeps tr
4 min read
set::clear in C++ STL
Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::clear() clear() function is used to remove all the elements of the
2 min read
multimap clear() function in C++ STL
The multimap clear() function is an inbuilt function in C++ STL which is used to remove all elements from the multimap container (which are destroyed), leaving the container with a size of 0. Syntax : mymultimap_name.clear() Parameters: This function does not take any arguments. Return Value: This function does not returns anything. The return type
2 min read
multiset clear() function in C++ STL
The multiset::clear() function is a built-in function in C++ STL which removes all elements from the multiset container. The final size of multiset container after removal is 0. Syntax: multiset_name.clear() Parameters: The function does not accept any parameter. Return Value: The function does not returns anything. Below programs illustrates the m
2 min read
unordered_set clear() function in C++ STL
The unordered_set::clear() function is a built-in function in C++ STL which is used to clear an unordered_set container. That is, this function removes all of the elements from an unordered_set and empties it. All of the iterators, pointers, and references to the container are invalidated. This reduces the size of the container to zero. Syntax: uno
2 min read
unordered_multiset clear() function in C++ STL
The unordered_multiset::clear() is a built-in function in C++ STL which clears the contents of the unordered_multiset container. The final size of the container after the call of the function is 0. Syntax: unordered_multiset_name.clear() Parameters: The function does not accept any parameter. Return Value: It returns nothing. Below programs illustr
2 min read
unordered_multimap clear() function in C++ STL
The unordered_multimap::clear() is a built-in function in C++ STL which clears the contents of the unordered_multimap container. The final size of the container after the call of the function is 0. Syntax: unordered_multimap_name.clear() Parameters: The function does not accept any parameter. Return Value: It returns nothing. Below programs illustr
2 min read
unordered_map clear in C++ STL
unordered_map::clear() function is used to remove all elements from the container. When this function is applied to unordered_map its size becomes zero. Syntax: unordered_map_name.clear() Parameters: This function does not accepts any parameter Return type: This function return nothing. Examples: Input: ump = { {1, 2}, {3, 4}, {5, 6}, {7, 8}} ump.c
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg