std::multimap::clear(3)

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

πŸ’… std::multimap::clear(3) β€” bestie edition πŸ’…

okay so this is the C++ Standard Libary manual page for std::multimap::clear(3), and honestly? It’s a little icon: the member function that clears out the whole container. main character energy, zero drama. πŸ’–


πŸŽ€ NAME πŸŽ€

std::multimap::clear - std::multimap::clear

yep, the name is the name β€” self-titled legend behavior. πŸ‘‘


✨ Synopsis ✨

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

okay so clear() erases all elements from the container. After this call, size() returns zero. and it’s noexcept since C++11 β€” slay. πŸ’…

Heads up, bestie: it invalidates any references, pointers, and iterators referring to contained elements. Any past-the-end iterator remains valid, though β€” noted and respected. 🫢

🧾 Parameters 🧾

(none) β€” literally no parameters, she takes zero arguments and serves. ✨


πŸ” Return value πŸ”

(none) β€” nothing comes back, because the clearing is the whole point. πŸ’β€β™€οΈ


⏱️ Complexity ⏱️

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

so it’s giving β€œtouch every element once,” not magic. no cap. 🌸


πŸ§ͺ Example πŸ§ͺ

// Run this code

#include <iostream>
#include <string_view>
#include <map>

void print_info(std::string_view rem, const std::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::multimap<int, char> container{{1, 'x'}, {2, 'y'}, {3, 'z'}};
    print_info("Before clear: ", container);
    container.clear();
    print_info("After clear: ", container);
}

πŸ“€ Output πŸ“€

Before clear: { [1]:x [2]:y [3]:z }
Size=3
After clear: { }
Size=0

the before-and-after is honestly so satisfying. πŸ’€βœ¨


🩹 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()'

they really said β€œdefine your terms, bestie,” and honestly? valid. 🫢


πŸ‘€ See also πŸ‘€

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

πŸ’– source/version/date: http://cppreference.com, 2024.06.10, std::multimap::clear(3) β€” mwah, bye bestie! πŸ’–

πŸ“¦ where this page comes from