std::set::clear(3)

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

πŸ’… std::set::clear(3) πŸ’…

okay bestie, welcome to std::set::clear(3) from the C++ Standard Libary manual β€” yes, β€œLibary” is literally in the header, and we are respecting the artifact πŸ’–.


🌸 NAME 🌸

std::set::clear - std::set::clear

that’s the NAME section and honestly it ate. no notes. πŸ’…


πŸ¦‹ Synopsis πŸ¦‹

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

so here’s the tea: she erases all elements from the container. after this call, size() returns zero. iconic behavior, fr fr ✨.

she invalidates any references, pointers, and iterators referring to contained elements. but β€” and this is important β€” any past-the-end iterator remains valid 🫢.


🌸 Parameters 🌸

(none)

no parameters, no drama. she takes nothing and still clears the whole vibe check πŸ’€.


πŸ’– Return value πŸ’–

(none)

returns nothing. she gives no return value, only closure. πŸŽ€


πŸ”₯ Complexity πŸ”₯

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

that’s the whole complexity tea. periodt. ⭐


✨ Example ✨

// Run this code

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

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

she really said β€œempty set, empty set, empty set” and the output ATE. πŸ“


πŸ’€ 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()'.

history was made, paperwork was filed, and the complexity got corrected. πŸ’…


πŸ‘€ See also πŸ‘€

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

like, if clear() is the full reset, erase is the pick-and-choose girlypop. πŸ¦‹


kisses bestie, this page is from http://cppreference.com, dated 2024.06.10, and it’s literally titled std::set::clear(3). bye and slay πŸ’œβœ¨

πŸ“¦ where this page comes from