std::list::clear(3)
π NAME π
std::list::clear - std::list::clear
Ok bestie, this is the page for std::list::clear(3) from the C++ Standard Libary β yes, thatβs the manual name in the header, and weβre keeping it exact. π
β¨ Synopsis β¨
void clear(); (noexcept since C++11)
This one is iconic: it erases all elements from the container. After this call, size() returns zero. π«§
It invalidates any references, pointers, and iterators referring to contained elements. Any past-the-end iterator remains valid β that one stays valid, no cap. β¨
π Parameters π
(none)β literally no arguments, bestie. π
π«Ά Return value π«Ά
(none)β it returns nothing; the flex is the deletion. π«Ά
π₯ Complexity π₯
Linear in the size of the container, i.e., the number of elements. So the work scales with how many elements are in there. π₯
π Example π
// Run this code
#include <iostream>
#include <string_view>
#include <list>
void print_info(std::string_view rem, const std::list<int>& v)
{
std::cout << rem << "{ ";
for (const auto& value : v)
std::cout << value << ' ';
std::cout << "}\n";
std::cout << "Size=" << v.size() << '\n';
}
int main()
{
std::list<int> container{1, 2, 3};
print_info("Before clear: ", container);
container.clear();
print_info("After clear: ", container);
}
π Output π
Before clear: { 1 2 3 }
Size=3
After clear: { }
Size=0
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. π
- DR:
LWG 2231β Applied to:C++11β Behavior as published: complexity guarantee was mistakenly omitted inC++11β Correct behavior: complexity reaffirmed as linear.
π¦ See also π¦
eraseβ erases elements (public member function)
Thatβs the whole page, bestie. Source/version:http://cppreference.comΒ· date:2024.06.10β mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms