std::experi...std::deque)(3)

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

πŸŽ€ std::experimental::erase(std::deque) πŸŽ€

okay bestie, so this page lives in the C++ Standard Library manual (yes, that one) β€” let me put you on to what it actually does πŸ’–

✨ NAME ✨

std::experimental::erase(std::deque) β€” literally just std::experimental::erase(std::deque), that's the whole name, she's not complicated πŸ’…


🌸 SYNOPSIS 🌸

First things first, she's defined in header <experimental/deque> β€” go include it or she won't show up, and that's on periodt.

template< class T, class A, class U >               (library fundamentals TS v2)
void erase( std::deque<T, A>& c, const U& value );

So what does she do? She erases all elements that compare equal to value from the container. That's the vibe check and some elements are failing it πŸ™ˆ

She's exactly equivalent to this line right here, no cap:

c.erase(std::remove(c.begin(), c.end(), value), c.end());.

it's giving erase-remove idiom but make it a one-liner πŸ¦‹


πŸ’œ PARAMETERS πŸ’œ

  • c β€” the container from which to erase, aka the deque we're cleaning out 🧹
  • value β€” the value to be removed, aka the one getting evicted πŸ‘‹

⭐ COMPLEXITY ⭐

Linear. That's it. That's the whole section. Lowkey iconic for how brief it is πŸ’€


πŸ”₯ EXAMPLE πŸ”₯

// Run this code

#include <experimental/deque>
#include <iostream>

auto show = [](const auto& container)
{
    for (auto e : container)
        std::cout << e;
    std::cout << '\n';
};

int main()
{
    std::deque<int> data{1, 1, 1, 4, 1, 1, 1, 2, 1, 1, 1};
    show(data);
    std::experimental::erase(data, 1);
    show(data);
}

look at that little lambda, she's so cute, she just prints everything with no separator and a newline at the end 🫢

🌈 OUTPUT 🌈

11141112111
42

ok so the 1s got absolutely obliterated and only the 4 and the 2 survived 😭 they really said main character energy and made it out alive πŸ‘‘


πŸ‘‘ SEE ALSO πŸ‘‘

  • remove β€” removes elements satisfying specific criteria
  • remove_if β€” (function template)
  • erase_if (std::deque) (library fundamentals 2 TS) β€” erases all elements satisfying a predicate from a std::deque (function template)

ngl bestie, remember this whole thing is under the library fundamentals TS v2 umbrella, so treat her as experimental, okay? that's literally in the name πŸ’€

and that's the tea πŸ“ β€” this page came from http://cppreference.com, dated 2024.06.10, under the title std::experi...std::deque)(3). go forth and erase βœ¨πŸ’–πŸŽ€

πŸ“¦ where this page comes from