std::erase_...d_multimap)(3)
π 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 returnstrueif the element should be erased. Like a little bouncer at the club door:truemeans 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
- 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