std::unorde...imap::clear(3)
π NAME π
std::unordered_multimap::clear β bestie, it's literally std::unordered_multimap::clear
So this page is from the C++ Standard Libary manual, and it's all about one tiny icon of a member function. Let's get into it π«Ά
β¨ Synopsis β¨
void clear() noexcept; (since C++11)
Ok so what it does: it erases all elements from the container. That's the whole job. After you call this, size() returns zero. Empty. Void. Nothing left. She cleared the group chat π
It invalidates any references, pointers, and iterators referring to contained elements. And heads up β it may also invalidate past-the-end iterators. So yeah, don't be holding onto those, ngl π
π Parameters π
(none) β she takes nothing, she gives nothing, she simply clears β¨
πΈ Return value πΈ
(none)</p purple> β no return value, bestie. It's void. It's giving "I did the thing and I don't need a trophy for it" energy π
π¦ Complexity π¦
Linear in the size of the container, i.e., the number of elements. So like, more stuff = more work. No cap.
π Example π
This is the code, run it if you want:
// Run this code
#include <iostream>
#include <string_view>
#include <unordered_map>
void print_info(std::string_view rem, const std::unordered_multimap<int, char>& v)
{
std::cout << rem << "{ ";
for (const auto& [key, value] : v)
std::cout << '[' << key << "]:" << value << ' ';
std::cout << "}\n";
std::cout << "Size=" << v.size() << '\n';
}
int main()
{
std::unordered_multimap<int, char> container{{1, 'x'}, {2, 'y'}, {3, 'z'}};
print_info("Before clear: ", container);
container.clear();
print_info("After clear: ", container);
}
β Possible output β
Before clear: { [1]:x [2]:y [3]:z }
Size=3
After clear: { }
Size=0
Look at her go π Size=3 β Size=0. The glow-down was the glow-up fr fr π
π§ Defect reports π§
So the following behavior-changing defect reports were applied retroactively to previously published C++ standards. History said "let me fix that" and honestly? Slay.
LWG 2550β Applied to C++11. As published, for unordered associative containers, it was unclear if complexity is linear in the number of elements or buckets. The correct behavior: clarified that it's linear in the number of elements. Clarity ate π
π See also π
eraseβ erases elements (public member function)
(iykyk β erase is the picky one, she takes specific elements. clear is the "burn it all down" girlie π₯)
and that's the whole page bestie, you're so welcome π«Ά
Source/version + date check: this one comes from http://cppreference.com, version 2024.06.10, and the page is std::unorde...imap::clear(3) β yes the header really is abbreviated like that, not me being lazy, that's just how it printed ππ Go forth and clear your containers responsibly, ily π
π¦ 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