std::experi...d_multimap)(3)

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

πŸ’– NAME πŸ’–

std::experimental::erase_if(std::unordered_multimap) β€” bestie, it's the erase_if for std::unordered_multimap, and it's literally a two-argument icon. Periodt.

So this page lives in the C++ Standard Library manual, and the vibe is "erase the stuff that matches your little predicate." Let's get into it πŸŽ€


✨ SYNOPSIS ✨

Okay so first things first, where does this live? It's defined in the header <experimental/unordered_map> β€” don't forget that, she's important 🫢

template< class Key, class T, class Hash, class KeyEqual,
class Alloc, class Pred >
void erase_if( std::unordered_multimap<Key, T, Hash,
KeyEqual, Alloc>& c, Pred pred );

Quick note: this one ships with library fundamentals TS v2 β€” that's the "technical specification" era, iykyk πŸ‘‘

🌸 What it actually does 🌸

It erases all elements that satisfy the predicate pred from the container c. That's it. That's the whole job. She's efficient.

And it's equivalent to this exact loop, no cap:

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

Not me showing you the loop so you can see the mechanics β€” note how when pred(*i) is true, i = c.erase(i) hands back the next iterator, and only the else branch does ++i. The way that's the whole reason this isn't a dumb for loop 😭


πŸ’… PARAMETERS πŸ’…

  • c β€” the container from which to erase. Your main character container πŸ’œ
  • pred β€” the predicate that determines which elements should be erased. She decides who stays and who gets yeeted 🌈

⭐ COMPLEXITY ⭐

Linear. Straightforward, no surprises, slay ✨


πŸ¦‹ EXAMPLE πŸ¦‹

Here's the whole thing, runnable as-is. Go off, bestie:

// Run this code
#include <experimental/unordered_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::unordered_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';
}

Lowkey obsessed with that little operator<< template β€” it's giving "I print pairs so you don't have to" πŸ“

🌟 Possible output 🌟

Note the word possible β€” std::unordered_multimap doesn't promise an order, so yours might come out different. Anyway, here's what you might see:

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

The odd keys got absolutely ate. Only the even ones survived, as they should πŸ’€


🧚 SEE ALSO 🧚

  • remove β€” removes elements satisfying specific criteria
  • remove_if β€” function template

Both iconic in their own right. Go read those next, iykyk πŸ”₯


πŸ‘‘ CATEGORY πŸ‘‘

  • Noindexed pages

(Yeah that's the category. She's a little mysterious. We don't ask πŸ™ˆ)


Heads up bestie: this is an experimental TS thing, so it lives under std::experimental:: β€” not plain std::. Don't @ me, that's just where she's defined πŸ’–

That's the whole page!! Written from the cppreference glow-up, dated 2024.06.10, straight from http://cppreference.com β€” go forth and erase with confidence πŸ«Άβœ¨πŸ’…

πŸ“¦ where this page comes from