std::experi...::multimap)(3)

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

✨ std::experimental::erase_if(std::multimap) ✨

okay bestie, quick vibe check: this is a page from the C++ Standard Libary manual (yes, that's how it's spelled up top, we don't fix, we don't judge πŸ’…). It's the 3 section page for std::experimental::erase_if(std::multimap) and honestly? it's giving "tiny utility, huge slay." πŸ¦‹


πŸŽ€ NAME πŸŽ€

std::experimental::erase_if(std::multimap) β€” literally the same thing twice, because that's the name, and the name is iconic. periodt. ✨


🌸 Synopsis 🌸

okay so the whole thing lives in the header <experimental/map>. write that down. πŸ“

template< class Key, class T, class Compare, class Alloc, class Pred >
void erase_if( std::multimap<Key, T, Compare, Alloc>& c, Pred pred );

and like, this is tagged (library fundamentals TS v2) β€” that's the version tea β˜• so don't come for me if you're on something else.

🫢 what it actually does

It erases all the elements that satisfy the predicate pred from the container. every single one. no crumbs left. 😭

It's equivalent to this little loop right here:

for (auto i = c.begin(), last = c.end(); i != last;)
{
    if (pred(*i))
        i = c.erase(i);
    else
        ++i;
}

like, that's literally it. that's the whole personality. main character energy in four lines. πŸ‘‘


πŸ’– Parameters πŸ’–

  • c β€” the container you're erasing from, aka the victim 🫢
  • pred β€” the predicate that decides which elements get erased. it's the judge, jury, and vibe checker. ⭐

⚑ Complexity ⚑

Linear. that's it. that's the whole section. lowkey the shortest paragraph on the page and honestly? respect. 🌈


πŸ”₯ Example πŸ”₯

the code is right here, run it, don't run it, idk, live your truth:

// Run this code

#include <experimental/map>
#include <iostream>

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

int main()
{
    std::multimap<int, char> data{{1, 'a'},{2, 'b'},{3, 'c'},{4, 'd'},
                                  {5, 'e'},{4, 'f'},{5, 'g'},{5, 'g'}};
    std::cout << "Original:\n" << data << '\n';
    std::experimental::erase_if(data, [](const auto& item)
    {
        return (item.first & 1) == 1;
    });
    std::cout << "Erase items with odd keys:\n" << data << '\n';
}

πŸ“ Output πŸ“

Original:
{{1, a}{2, b}{3, c}{4, d}{4, f}{5, e}{5, g}{5, g}}
Erase items with odd keys:
{{2, b}{4, d}{4, f}}

not the odd keys getting absolutely obliterated 😭 they really said "not you" and meant it πŸ’€


πŸ‘€ See also πŸ‘€

  • remove β€” removes elements satisfying specific criteria
  • remove_if (function template) β€” bestie cousin of the above, also a function template

🧚 Category 🧚

  • Noindexed pages

(that's the category, ngl it's giving "left on read" but we keep it, we don't erase it β€” see what I did there πŸ™ˆ)


heads up bestie: this page is tagged with (library fundamentals TS v2), so it's a TS thing, a technical specification, not the fully merged mainline standard. read the tag, know the tag, love the tag. πŸ’œ

okay that's the whole page!! πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«Ά

cute little sign-off for you: this came from http://cppreference.com, dated 2024.06.10, and it's the std::experi...::multimap)(3) page β€” yes the title is truncated with dots in the header, slay, we reproduce it faithfully, no cap. byeee πŸ‘‘β­

πŸ“¦ where this page comes from