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

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

🌸 NAME 🌸

std::unordered_multiset::clear β€” the member function that wipes every element out of your unordered multiset. This is the std::unorde...iset::clear(3) page from the C++ Standard Libary manual, and yes bestie, that’s literally how the manual name is spelled up top. No cap.


✨ Synopsis ✨

void clear() noexcept;

Available since C++11. It’s giving β€œone call and the whole container is gone” energy.

Okay so here’s the tea: it erases all elements from the container. After this call, size() returns zero. Literally zero. Empty. Nothing left. πŸ’…

Heads up, bestie: it invalidates any references, pointers, and iterators referring to contained elements. It may also invalidate past-the-end iterators. So don’t be out here using stale iterators after the clear β€” that’s not the vibe.

πŸ’… Parameters πŸ’…

  • (none) β€” no parameters, fr fr. It takes nothing.

πŸŽ€ Return value πŸŽ€

  • (none) β€” it returns nothing. Not even a little guy. Not even a vibe check.

πŸ”₯ Complexity πŸ”₯

Linear in the size of the container, i.e., the number of elements.


🌈 Example 🌈

// Run this code

#include <iostream>
#include <string_view>
#include <unordered_set>

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

πŸ’€ Defect reports πŸ’€

The following behavior-changing defect reports were applied retroactively to previously published C++ standards:

  • DR 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

πŸ‘‘ See also πŸ‘‘

  • erase β€” erases elements (public member function)

xoxo, your local C++ bestie πŸ’– β€” source/version and date: http://cppreference.com, 2024.06.10, page std::unorde...iset::clear(3). Periodt. 🫢

πŸ“¦ where this page comes from