std::erase_...::multimap)(3)
π 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 returnstrueif 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
- 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