std::multiset::clear(3)

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

🌸 NAME 🌸

Okay so, bestie, this is std::multiset::clear(3) from the C++ Standard Libary manual, section 3, and the NAME line is lowkey just std::multiset::clear - std::multiset::clear β€” which is giving β€œI am the moment, I am the brand” πŸ‘‘βœ¨


✨ Synopsis ✨

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

It erases all elements from the container. After this call, size() returns zero. Periodt πŸ’…

It also invalidates any references, pointers, and iterators referring to contained elements. Any past-the-end iterator remains valid. So like… the end iterator is safe, but the element-havers are NOT 😭


πŸŽ€ Parameters πŸŽ€

(none) β€” it takes absolutely nothing, she works alone, main character energy fr fr πŸ¦‹


🫢 Return value 🫢

(none) β€” also nothing. No return, no drama, just vibes and deletion πŸ’€


πŸ”₯ Complexity πŸ”₯

Linear in the size of the container, i.e., the number of elements. That’s the whole complexity tea β˜•βœ¨


🎬 Example 🎬

Run this code, bestie:

// Run this code

#include <iostream>
#include <string_view>
#include <set>

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

Not the size going from 3 to 0 😭 she really said β€œclear” and cleared πŸ’…βœ¨


😭 Defect reports 😭

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

  • DR: LWG 224 Β· Applied to: C++98 Β· Behavior as published: the complexity was log(size()) + N, but N was not defined Β· Correct behavior: corrected to 'linear in size()' 🫢

πŸ‘€ See also πŸ‘€

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

Source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::multiset::clear(3) β€” okay bye bestie, stay slaying and keep those past-the-end iterators valid πŸŒˆπŸ’œβœ¨

πŸ“¦ where this page comes from