std::experi...td::vector)(3)

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

✨ std::experimental::erase_if(std::vector) ✨

okay bestie, welcome to the manpage for std::experimental::erase_if(std::vector) β€” yes that's the whole name, it's giving "my parents gave me a government name" energy πŸ’…. This is part of the C++ Standard Library, and no cap, it's lowkey iconic.


🌸 NAME 🌸

std::experimental::erase_if(std::vector) β€” literally the function we're about to obsess over, fr fr.


πŸ¦‹ Synopsis πŸ¦‹

so first things first, this baddie lives in the header <experimental/vector>. periodt.

template< class T, class Alloc, class Pred >           (library fundamentals TS v2)
void erase_if( std::vector<T, Alloc>& c, Pred pred );

what does it do? it erases all elements that satisfy the predicate pred from the container. it's giving "if you don't fit the vibe, you're out" 😭. and it's equivalent to:

c.erase(std::remove_if(c.begin(), c.end(), pred), c.end());

that's it. that's the whole function. she's simple, she's clean, she's efficient. we stan ✨.


πŸŽ€ Parameters πŸŽ€

  • c β€” the container from which to erase. this is the main character, the vector herself πŸ’–.
  • pred β€” predicate that determines which elements should be erased. think of it like the bouncer at the club β€” it decides who gets kicked out πŸ‘‘.

🧚 Complexity 🧚

Linear. ngl, she's not doing anything fancy algorithmically β€” just vibing through the elements once. efficient queen 🌈.


⭐ Example ⭐

okay bestie, let's cook. here's the full code:

// Run this code

#include <experimental/vector>
#include <iostream>

template<typename Os, typename Container>
inline Os& operator<<(Os& os, Container const& container)
{
    os << "{ ";
    for (const auto& item : container)
        os << item << ' ';
    return os << '}';
}

int main()
{
    std::vector<int> data{3, 3, 4, 5, 5, 6, 6, 7, 2, 1, 0};
    std::cout << "Original:\n" << data << '\n';
    auto divisible_by_3 = [](auto const& x) { return (x % 3) == 0; };
    std::experimental::erase_if(data, divisible_by_3);
    std::cout << "Erase all items divisible by 3:\n" << data << '\n';
}

and the output? slay:

Original:
{ 3 3 4 5 5 6 6 7 2 1 0 }
Erase all items divisible by 3:
{ 4 5 5 7 2 1 }

see how all the 3s and 6s and 0 got yeeted? that's the power of erase_if. you're welcome πŸ’….


πŸ”₯ See also πŸ”₯

the friend group of related functions, iykyk:

  • remove β€” removes elements satisfying specific criteria. the og.
  • remove_if β€” (function template) the one that takes a predicate. very much giving "living in remove's shadow but still iconic."
  • erase (std::vector) β€” erases all elements equal to a specific value from a std::vector. (library fundamentals 2 TS) (function template). the cousin who does things by value instead of vibe.

πŸ’œ Category πŸ’œ

  • Noindexed pages. yeah she's not in the index but she's still in our hearts 🫢.

🌸 so that's the whole manpage, bestie. she's a cute little experimental function that does one thing and does it well. no cap, go use her responsibly. also don't forget she's part of the library fundamentals TS v2 β€” that's the technical spec, look it up.

ngl if you're using this in production, make sure your compiler supports the experimental namespace. it's experimental for a reason 😭.

πŸ’– signed, sealed, delivered β€” cppreference.com, 2024.06.10. the date is giving "she was documented on a summer day and the vibes are immaculate." πŸ’–

✨ now go erase some elements and slay the day, bestie. ✨

πŸ“¦ where this page comes from