std::deque::clear(3)

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

πŸ’– NAME πŸ’–

std::deque::clear β€” std::deque::clear

okay so bestie, welcome to std::deque::clear(3) from the C++ Standard Libary manual. Yes, the NAME line is literally the same thing twice β€” it’s giving minimal, it’s giving icon behavior πŸ’….


✨ Synopsis ✨

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

This is the whole signature, no cap. It’s marked noexcept since C++11 exactly like that ✨.

What it does: Erases all elements from the container. After this call, size() returns zero. Periodt.

⚠️ Invalidates any references, pointers, and iterators referring to contained elements. Any past-the-end iterators are also invalidated. So yeah, don’t be holding onto old iterators like they’re still valid β€” they are not, bestie 😭.

🫢 Parameters 🫢

  • (none) β€” no arguments, no stress, no little parameter besties to feed.

πŸŽ€ Return value πŸŽ€

  • (none) β€” it returns literally nothing. It just does the clear and dips.

🌸 Complexity 🌸

Linear in the size of the container, i.e., the number of elements. So the bigger the deque, the more work it’s doing β€” fr fr.


πŸ¦‹ Example πŸ¦‹

The page gives the cue // Run this code, and then this exact code drops:

#include <iostream>
#include <string_view>
#include <deque>

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

ate. no crumbs. the deque really said β€œempty era” 🫢✨


πŸ’… 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)

source/date sign-off: http://cppreference.com Β· 2024.06.10 πŸ’–βœ¨

πŸ“¦ where this page comes from