C++ Forward_list::clear() Function



The C++ std::forward_list::clear() function is used to erase all elements of the forward_list.

It destroys the forward_list by removing all elements from the forward_list and sets the size of the forward_list to 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::forward_list::clear() function −

void clear();

Parameters

  • It does not accept any parameter.

Return value

This function does not return any value.

Example 1

If the forward_list is a int-type, the clear() function eases all the elements from the forward_list.

In the following program, we are using the C++ std::forward_list::clear() function to erase all the elements of the current forward_list {10, 20, 30, 40, 50}, and this function calls the size of the forward list will be zero.

#include<iostream>
#include<forward_list>
using namespace std;
int main(){
   //create a forward_list
   forward_list<int> num_list = {10, 20, 30, 40, 50};
   cout<<"The forward_list contents before the clear operation: "<<endl;
   for(int n : num_list) {
      cout<<n<<endl;
   }
   cout<<"The size of the forward_list before clear operation: ";
   int size = distance(num_list.begin(), num_list.end());
   cout<<size<<endl;
   //using the clear() function
   num_list.clear();
   int new_size = distance(num_list.begin(), num_list.end());
   cout<<"The size of forward_list after clear operation: "<<new_size;
}

Output

Following is the output of the above program −

The forward_list contents before the clear operation: 
10
20
30
40
50
The size of the forward_list before clear operation: 5
The size of forward_list after clear operation: 0

Example 2

If the forward_list is a char-type, this function erases all the elements from the forward_list.

Following is another example of the C++ std::forward_list::clear() function. Here, we are creating a forward_list(type char) named char_list with the contents {'A', 'B', 'C', 'D', 'E'}. Then, using the clear() function, we are trying to erase all elements of this forward_list.

#include<iostream>
#include<forward_list>
using namespace std;
int main(){
   //create a forward_list
   forward_list<int> char_list = {'A', 'B', 'C', 'D', 'E'};
   cout<<"The forward_list contents before the clear operation: "<<endl;
   for(char c : char_list) {
      cout<<c<<endl;
   }
   cout<<"The size of the forward_list before clear operation: ";
   int size = distance(char_list.begin(), char_list.end());
   cout<<size<<endl;
   //using the clear() function
   char_list.clear();
   int new_size = distance(char_list.begin(), char_list.end());
   cout<<"The size of forward_list after clear operation: "<<new_size;
}

Output

This will generate the following output −

The forward_list contents before the clear operation: 
A
B
C
D
E
The size of the forward_list before clear operation: 5
The size of forward_list after clear operation: 0

Example 3

Apart from the int-type and char-type forward_list elements, we can also erase the string-type forward_list elements.

In this example, we are creating a forward_list(type string) named languages with the contents {"Java", "HTML", "CSS", "JavaScript"}. Using the clear() function, we are trying to erase all elements of this forward_list.

#include<iostream>
#include<forward_list>
using namespace std;
int main(){
   //create a forward_list
   forward_list<string> languages = {"Java", "HTML", "CSS", "JavaScript"};
   cout<<"The forward_list contents before the clear operation: "<<endl;
   for(string l : languages) {
      cout<<l<<endl;
   }
   cout<<"The size of the forward_list before clear operation: ";
   int size = distance(languages.begin(), languages.end());
   cout<<size<<endl;
   //using the clear() function
   languages.clear();
   int new_size = distance(languages.begin(), languages.end());
   if(new_size == 0) {
      cout<<"The forward_list has no elements and size is: "<<new_size;
   }else{
      cout<<"The forward_list is non_empty and size is: "<<new_size;
   }
}

Output

The above program produces the following output −

The forward_list contents before the clear operation: 
Java
HTML
CSS
JavaScript
The size of the forward_list before clear operation: 4
The forward_list has no elements and size is: 0
Advertisements