The clear() Method is used to remove all the elements from the List.
#include <iostream> #include <list> using namespace std; int main() { list<string> x = {"Mohan", "Kriti", "Salim"}; x.clear(); for (string data : x) { cout << data << endl; } return 0; }
So, in the above code we have created a List and initialised to the variable x.
list<string> x = {"Mohan", "Kriti", "Salim"};
Below is how the values are positioned in the List,
Next, we have used the clear() method that removes all the elements from the List making the List empty.
x.clear();
And we get an empty List as output,