
- 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>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
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