std::erase_...d_multimap)(3)

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

πŸ’– NAME πŸ’–

std::erase_if(std::unordered_multimap) β€” okay so this is literally the erase-if overload for std::unordered_multimap, and it's about to eat πŸ’…


✨ SYNOPSIS ✨

It lives in the header <unordered_map> (yes bestie, the multimap header is <unordered_map>, that's not a typo, that's just how the standard library does things 🫢).

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

since C++20 🌈 β€” mark your calendars, this one's a modern icon.

So what does it do? It erases all elements that satisfy the predicate pred from c. That's it. That's the whole vibe. ✨

And it's equivalent to this exact chunk of code right here β€” no cap, this is the literal definition:

auto old_size = c.size();
for (auto first = c.begin(), last = c.end(); first != last;)
{
    if (pred(*first))
        first = c.erase(first);
    else
        ++first;
}
return old_size - c.size();

Notice how it's lowkey careful with first β€” erase returns the next iterator, otherwise you just ++first. That's the whole trick, iykyk 🧚


πŸŽ€ PARAMETERS πŸŽ€

  • c β€” the container we're erasing from, the main character of this operation πŸ‘‘
  • pred β€” the predicate that returns true if the element should be erased. Like a little bouncer at the club door: true means you're out πŸšͺπŸ’€

⭐ RETURN VALUE ⭐

The number of erased elements. A little receipt for your trouble, fr fr 🧾✨


🌸 COMPLEXITY 🌸

Linear. Not quadratic, not exponential, just linear, we love a consistent queen πŸ’…


πŸ¦‹ EXAMPLE πŸ¦‹

Run this code, bestie, it's giving tutorial energy:

#include <iostream>
#include <unordered_map>

void println(auto rem, auto const& container)
{
    std::cout << rem << '{';
    for (char sep[]{0, ' ', 0}; const auto& [key, value] : container)
        std::cout << sep << '{' << key << ", " << value << '}', *sep = ',';
    std::cout << "}\n";
}

int main()
{
    std::unordered_multimap<int, char> data
    {
        {1, 'a'}, {2, 'b'}, {3, 'c'}, {4, 'd'},
        {5, 'e'}, {4, 'f'}, {5, 'g'}, {5, 'g'},
    };
    println("Original:\n", data);

    const auto count = std::erase_if(data, [](const auto& item)
    {
        auto const& [key, value] = item;
        return (key & 1) == 1;
    });

    println("Erase items with odd keys:\n", data);
    std::cout << count << " items removed.\n";
}

🌈 POSSIBLE OUTPUT 🌈

Heads up, it says possible output for a reason β€” unordered containers don't promise you any particular order, so your run might shuffle things around. The counts though? Those are locked in πŸ”’
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}}
5 items removed.

The way it just deleted every odd key and reported back 5... obsessed 😭πŸ”₯


πŸ’œ SEE ALSO πŸ’œ

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

Okay that's the whole page, you ate that πŸ“βœ¨ Go forth and erase responsibly. Sending you good vibes and linear time complexity πŸ«ΆπŸ’–

β€” xoxo, straight from cppreference.com, version 2024.06.10. Catch you on the next manpage, bestie πŸ’…πŸŽ€

πŸ“¦ where this page comes from