![The C++ Standard Library](/cpp_standard_library/images/cpp-mini-lib-logo.jpg)
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ List::clear() Function
The C++ std::list::clear() function is used to erase all the elements from the list( or container).
When this function is invoked the size() function returns the list size as zero. The return type of this function is void which implies that it does not return any value.
Syntax
Following is the syntax of the C++ std::list::clear() function −
void clear();
Parameters
- It does not accepts any parameter.
Return Value
This function does not return anything.
Example 1
In the following program, we are using the C++ std::list::clear() function to clear the current list {10, 20, 30, 40, 50}.
#include<iostream> #include<list> using namespace std; int main(void) { //create an integer list list<int> lst = {10, 20, 30, 40, 50}; cout<<"The list elements are: "; for(int l: lst) { cout<<l<<" "; } cout<<"\nInitial size of list: "<<lst.size(); //use clear() method lst.clear(); cout<<"\nAfter perform the clear() function the list size is: "<<lst.size(); }
Output
The above program produces the following output −
The list elements are: 10 20 30 40 50 Initial size of list: 5 After perform the clear() function the list size is: 0
Example 2
Following is another example of the C++ std::list::clear() function, here, we are creating a list( type char) with the value {'+', '@', '#', '$','%'}, and using this function, we are trying to clear this list.
#include<iostream> #include<list> using namespace std; int main(void) { //create char type list list<char> lst = {'+', '@', '#', '$','%'}; cout<<"The list elements are: "; for(char l: lst) { cout<<l<<" "; } cout<<"\nInitial size of list: "<<lst.size(); //use clear() method lst.clear(); cout<<"\nAfter perform the clear() function the list size is: "<<lst.size(); }
Output
Following is the output of the above program −
The list elements are: + @ # $ % Initial size of list: 5 After perform the clear() function the list size is: 0
Example 3
If the current list is a string type.
In this program, we create a list(type string) with the value {'Java', 'HTML', 'CSS', 'Angular'}. Then, using the C++ std::list::clear() function, we are trying to clear this list.
#include<iostream> #include<list> using namespace std; int main(void) { list<string> lst = {"Java", "HTML", "CSS", "Angular"}; cout<<"The list elements are: "; for(string l: lst) { cout<<l<<" "; } cout<<"\nInitial size of list: "<<lst.size(); //use clear() method lst.clear(); cout<<"\nAfter perform the clear() function the list size is: "<<lst.size(); }
Output
On executing the above program, it will produce the following output −
The list elements are: Java HTML CSS Angular Initial size of list: 4 After perform the clear() function the list size is: 0