std::experi...td::vector)(3)
β¨ 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 astd::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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms