std::experi...d_multimap)(3)
π 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 criteriaremove_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 understd::experimental::β not plainstd::. 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
- 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