std::flat_multiset::clear(3)

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

πŸ’… std::flat_multiset::clear

okay bestie so this is the clear member of std::flat_multiset from the C++ Standard Library β€” yes, the whole std situation, we're in C++ Standard Libary territory (their spelling, not mine, iykyk 🫢). It's giving "make it empty, make it iconic."


✨ Synopsis ✨

void clear() noexcept;  (since C++23)

So like, clear() is a thing since C++23, and it's noexcept β€” meaning it will not be throwing hands. Ever. Periodt.

What it does: it erases all elements from the container adaptor. Every single one. Gone. After this call, size() returns zero β€” empty, fresh, brand new era πŸ’….

🚨 real talk though: it invalidates any references, pointers, and iterators referring to contained elements. All of them. They're not coming back, bestie β€” do not use them after this, fr fr 😭.

πŸŽ€ Parameters πŸŽ€

(none) β€” literally nothing, no arguments, she takes zero input, highkey low-maintenance πŸ¦‹


🌸 Return value 🌸

(none) β€” she gives you nothing back, because she already gave you everything (an empty container) πŸ’–


⭐ Complexity ⭐

Linear in the size of the container adaptor, i.e., the number of elements. So the more stuff you got in there, the more work it does β€” no cap, it's proportional πŸ“ˆ


πŸ”₯ Example πŸ”₯

// Run this code

#include <iostream>
#include <string_view>
#include <flat_set>

void print_info(std::string_view rem, const std::flat_multiset<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::flat_multiset<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

the way the size just goes from 3 to 0… she ate and left no crumbs πŸ“βœ¨


πŸ‘‘ See also πŸ‘‘

  • erase β€” erases elements (public member function) β€” like clear but she's picky about which ones πŸ’…

so yeah, that's the whole vibe. go forth and empty your containers, bestie πŸ§šπŸ’œ

this lil page is courtesy of http://cppreference.com, dated 2024.06.10 β€” std::flat_multiset::clear(3), mwah πŸ’‹
πŸ“¦ where this page comes from