std::unordered_set::clear(3)
π std::unordered_set::clear β C++ Standard Library, bestie edition
Okay so we're in the C++ Standard Library pages (std::unordered_set::clear(3)), and honestly? This one's a whole mood reset button for your container. Let's get into it β¨
πΈ NAME
std::unordered_set::clear β the function that literally just empties your whole set. That's it. That's the function.
π Synopsis
void clear() noexcept; (since C++11)
Bestie. It erases all elements from the container. Every last one. After this call, size() returns zero β like, actually zero, no crumbs left π
Also, heads up: it invalidates any references, pointers, and iterators referring to contained elements. And it may also invalidate past-the-end iterators. So if you were holding onto an iterator like it was your emotional support bestieβ¦ it's gone now π
π¦ Parameters
(none) β she takes nothing, gives nothing, just vibes π«Ά
π Return value
(none) β void queen behavior, no receipts, no notes π
β Complexity
Linear in the size of the container, i.e., the number of elements. So the more stuff you have in there, the longer the goodbye takes. No shortcuts, no cap.
π Example
Okay so here's the actual code, run this:
// Run this code
#include <iostream>
#include <string_view>
#include <unordered_set>
void print_info(std::string_view rem, const std::unordered_set<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::unordered_set<int> container{1, 2, 3};
print_info("Before clear: ", container);
container.clear();
print_info("After clear: ", container);
}
β¨ Possible output
Before clear: { 1 2 3 }
Size=3
After clear: { }
Size=0
Not the Size=0 at the end π she really said "empty" and meant it. Iconic.
π§ Defect reports
So the following behavior-changing defect reports were applied retroactively to previously published C++ standards. Like a patch note but for the standard itself π
LWG 2550β applied to C++11. Behavior as published: for unordered associative containers, unclear if complexity is linear in the number of elements or buckets. Correct behavior: clarified that it's linear in the number of elements. Thank you LWG, we were all confused and now we're not π
π See also
eraseβ erases elements (public member function) β ifclearis the "delete the whole chat" button,eraseis the "remove one specific message" button. Use accordingly π¦
β¨ and that's the page, bestie β¨ β sourced from http://cppreference.com, dated 2024.06.10, from the std::unordered_set::clear(3) page. Go forth and clear responsibly ππΈ
π¦ 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