std::list::clear(3)

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

πŸ’– NAME πŸ’–

std::list::clear - std::list::clear

Ok bestie, this is the page for std::list::clear(3) from the C++ Standard Libary β€” yes, that’s the manual name in the header, and we’re keeping it exact. πŸ’…


✨ Synopsis ✨

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

This one is iconic: it erases all elements from the container. After this call, size() returns zero. 🫧

It invalidates any references, pointers, and iterators referring to contained elements. Any past-the-end iterator remains valid β€” that one stays valid, no cap. ✨

πŸŽ€ Parameters πŸŽ€

  • (none) β€” literally no arguments, bestie. πŸŽ€

🫢 Return value 🫢

  • (none) β€” it returns nothing; the flex is the deletion. 🫢

πŸ”₯ Complexity πŸ”₯

Linear in the size of the container, i.e., the number of elements. So the work scales with how many elements are in there. πŸ”₯

🌈 Example 🌈

// Run this code

#include <iostream>
#include <string_view>
#include <list>

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

πŸ’… Defect reports πŸ’…

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

  • DR: LWG 2231 β€” Applied to: C++11 β€” Behavior as published: complexity guarantee was mistakenly omitted in C++11 β€” Correct behavior: complexity reaffirmed as linear.

πŸ¦‹ See also πŸ¦‹

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

That’s the whole page, bestie. Source/version: http://cppreference.com Β· date: 2024.06.10 β€” mwah πŸ’‹βœ¨
πŸ“¦ where this page comes from