std::experi...::multimap)(3)
β¨ std::experimental::erase_if(std::multimap) β¨
okay bestie, quick vibe check: this is a page from the C++ Standard Libary manual (yes, that's how it's spelled up top, we don't fix, we don't judge π
). It's the 3 section page for std::experimental::erase_if(std::multimap) and honestly? it's giving "tiny utility, huge slay." π¦
π NAME π
std::experimental::erase_if(std::multimap) β literally the same thing twice, because that's the name, and the name is iconic. periodt. β¨
πΈ Synopsis πΈ
okay so the whole thing lives in the header <experimental/map>. write that down. π
template< class Key, class T, class Compare, class Alloc, class Pred >
void erase_if( std::multimap<Key, T, Compare, Alloc>& c, Pred pred );
and like, this is tagged (library fundamentals TS v2) β that's the version tea β so don't come for me if you're on something else.
π«Ά what it actually does
It erases all the elements that satisfy the predicate pred from the container. every single one. no crumbs left. π
It's equivalent to this little loop right here:
for (auto i = c.begin(), last = c.end(); i != last;)
{
if (pred(*i))
i = c.erase(i);
else
++i;
}
like, that's literally it. that's the whole personality. main character energy in four lines. π
π Parameters π
cβ the container you're erasing from, aka the victim π«Άpredβ the predicate that decides which elements get erased. it's the judge, jury, and vibe checker. β
β‘ Complexity β‘
Linear. that's it. that's the whole section. lowkey the shortest paragraph on the page and honestly? respect. π
π₯ Example π₯
the code is right here, run it, don't run it, idk, live your truth:
// Run this code
#include <experimental/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::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';
}
π 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}}
not the odd keys getting absolutely obliterated π they really said "not you" and meant it π
π See also π
removeβ removes elements satisfying specific criteriaremove_if(function template) β bestie cousin of the above, also a function template
π§ Category π§
- Noindexed pages
(that's the category, ngl it's giving "left on read" but we keep it, we don't erase it β see what I did there π)
heads up bestie: this page is tagged with (library fundamentals TS v2), so it's a TS thing, a technical specification, not the fully merged mainline standard. read the tag, know the tag, love the tag. π
okay that's the whole page!! πβ¨ππ¦πΈπ π«Ά
cute little sign-off for you: this came from http://cppreference.com, dated 2024.06.10, and it's the std::experi...::multimap)(3) page β yes the title is truncated with dots in the header, slay, we reproduce it faithfully, no cap. byeee πβ
π¦ 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