C++ std::list::clear
The std::list::clear function removes all elements from a std::list, leaving it with a size of zero. This operation does not change the capacity of the container, but it ensures that all elements are destroyed. After calling clear, the list will be empty.
Syntax of std::list::clear
void clear();
Parameters
This function does not accept any parameters.
Return Value
This function does not return a value. It modifies the list in place by removing all its elements.
Exceptions
The std::list::clear function does not throw exceptions. It provides a no-throw guarantee because it only destroys the elements and modifies internal pointers.
Examples for std::list::clear
Example 1: Clearing a List
This example demonstrates how to use clear to remove all elements from a list:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> myList = {10, 20, 30};
std::cout << "List contents before clear: ";
for (const auto& elem : myList) {
std::cout << elem << " ";
}
std::cout << std::endl;
// Clear the list
myList.clear();
std::cout << "List contents after clear: ";
for (const auto& elem : myList) {
std::cout << elem << " ";
}
std::cout << std::endl;
std::cout << "List size after clear: " << myList.size() << std::endl;
return 0;
}
Explanation:
- A
std::listnamedmyListis initialized with the elements{10, 20, 30}. - The contents of
myListare printed to show the elements before clearing. - The
clearfunction is called to remove all elements from the list. - After clearing, the contents of
myListare printed again, showing that the list is now empty. - The
size()function is used to verify that the size of the list is zero.
Output:
List contents before clear: 10 20 30
List contents after clear:
List size after clear: 0
Example 2: Clearing an Already Empty List
This example demonstrates that calling clear on an already empty list does not cause any issues:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> myList;
// Clear the empty list
myList.clear();
std::cout << "List size after clearing an empty list: " << myList.size() << std::endl;
return 0;
}
Explanation:
- A
std::listnamedmyListis created and is initially empty. - The
clearfunction is called on the empty list. This operation is safe and does not cause any issues. - The
size()function is used to confirm that the list remains empty after theclearoperation.
Output:
List size after clearing an empty list: 0
Example 3: Clearing a List of Complex Objects
This example demonstrates how clear works with a list containing complex objects:
Program
#include <iostream>
#include <list>
#include <string>
struct Person {
std::string name;
int age;
Person(const std::string& name, int age) : name(name), age(age) {}
};
int main() {
std::list<Person> people;
// Add some Person objects to the list
people.emplace_back("Arjun", 30);
people.emplace_back("Ram", 25);
std::cout << "List contents before clear: " << std::endl;
for (const auto& person : people) {
std::cout << person.name << " (" << person.age << " years old)" << std::endl;
}
// Clear the list
people.clear();
std::cout << "List size after clear: " << people.size() << std::endl;
return 0;
}
Explanation:
- A
Personstruct is defined with two members:name(a string) andage(an integer). - A
std::listofPersonobjects is created, and two objects (“Arjun” and “Ram”) are added to the list. - The
clearfunction is called to remove all elements from the list. - The
size()function is used to verify that the list is empty after clearing.
Output:
List contents before clear:
Arjun (30 years old)
Ram (25 years old)
List size after clear: 0
