std::unorde...imap::clear(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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