std::erase_...::multimap)(3)

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

πŸ’… NAME πŸ’…

std::erase_if(std::multimap) β€” bestie, this is the glow-up function that specifically targets std::multimap, and its whole personality is "erase everything that matches, no questions asked". We're in the C++ Standard Library manual, section 3, and yes this is that page. πŸŽ€


✨ Synopsis ✨

Defined in header <map> β€” don't forget it, she lives there. 🌸

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

(since C++20) β€” yeah she's a newer icon, don't come at her with a C++17 compiler, it's not gonna go well 😭

So what does she do? She erases all elements that satisfy the predicate pred from c. That's it, that's the whole vibe. One pass, no drama.

And she's equivalent to this little loop right here β€” literally the same energy, just packaged cuter:

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();

πŸ¦‹ Parameters πŸ¦‹

  • c β€” the container you're erasing from. Your target. Main character container. πŸ‘‘
  • pred β€” the predicate, aka the vibe check: it returns true if the element should be erased. true = gone bestie. πŸ’€

🌈 Return value 🌈

She returns the number of erased elements. So you always know exactly how many exes got removed. πŸ”₯


⭐ Complexity ⭐

Linear. One smooth pass through the container, no chaotic quadratic behavior. Slay. ✨


πŸ“ Example πŸ“

Run this code πŸ‘‡ (yes, fr fr, it's runnable, go off)
#include <iostream>
#include <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::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";
}

πŸ’œ 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}}
5 items removed.

Look at her go 😭 odd keys said bye, 5 items removed, the count came back correct, we love a consistent queen. 🫢


🧚 See also 🧚

  • remove, remove_if (function template) β€” removes elements satisfying specific criteria. The classic duo. Different flavor, same "get out of my container" energy. πŸ’…

Ok that's the whole page bestie, you're so welcome πŸ’– This one's from http://cppreference.com and she's dated 2024.06.10 β€” go erase something (a container element, not a person) πŸŽ€βœ¨

πŸ“¦ where this page comes from