std::erase_...::flat_map)(3)
π std::erase_if(std::flat_map) β C++ Standard Library, bestie edition
Okay so we're deep in std::flat_map territory, and ngl this one is a little cutie. It's all about yeeting elements out of a std::flat_map when they match whatever you say they match. No cap, it's giving "spring cleaning but make it templated." π§Ήβ¨
πΈ NAME πΈ
std::erase_if(std::flat_map) β literally std::erase_if(std::flat_map), that's the whole name, she's not complicated π
β SYNOPSIS β
Defined in header <flat_map>:
template< class Key, class T, class Compare, class KeyContainer,
class MappedContainer,
class Pred >
std::flat_map<Key, T, Compare, KeyContainer,
(since C++23)
MappedContainer>::size_type
erase_if( std::flat_map<Key, T, Compare, KeyContainer,
MappedContainer>& c,
Pred pred );
𫢠Heads up: this is since C++23, so if your compiler is acting brand new and doesn't know what a flat_map is⦠that's why, bestie.
π¦ DESCRIPTION π¦
So what does she actually do? She erases all elements that satisfy the predicate pred from c. That's it. That's the whole job. Iconic behaviour, honestly. π
And when do we say an element is "satisfied" by the predicate? The predicate pred is satisfied if this expression:
bool(pred(std::pair<const Key&, const T&>(e)))
is true, where e is some element in c. So like, the predicate gets handed a std::pair of const-refs to the key and the value, and if it says true, that element is gone. Bye. ππ
π₯ Important, don't skip this:KeyandTmust be MoveAssignable. Otherwise, the behavior is undefined. And undefined behaviour is NOT the cute kind of surprise, it's the "your program does a little interpretive dance and then segfaults" kind. So yeah. Make sure they're MoveAssignable, mmkay? π
π PARAMETERS π
cβ the container adaptor from which to erase. This is the flat_map getting the glow-up.predβ the predicate that returnstrueif the element should be erased. Basically the bouncer at the door:truemeans you're out. πͺ
β¨ RETURN VALUE β¨
The number of erased elements. She's polite like that β she tells you exactly how many people she had to escort out. π
π COMPLEXITY π
Exactly c.size() applications of the predicate pred. Not more, not fewer, exactly that many. Fr fr. So it's linear, bestie, no sneaky surprises. π
π EXCEPTIONS π
If erase_if throws, c remains in a valid but unspecified (maybe empty) state.
π Read that again: maybe empty. The container is still alive and well-formed, she's just not gonna promise you what's inside her. Could be everything, could be nothing. Main character energy only.
π§ NOTES π§
The algorithm is stable. Which means: the order of the elements that are not deleted remains unchanged. Nobody who survived the purge gets shuffled around. Respect the survivors. π«Άβ¨
π EXAMPLE π
// Run this code
#include <iostream>
#include <flat_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::flat_map<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}, {5, e}}
Erase items with odd keys:
{{2, b}, {4, d}}
3 items removed.
See how the odd keys 1, 3, 5 all got kicked out and the even ones stayed in their original order? That's the stability doing its thing. We love to see it. πβ¨
π SEE ALSO π
remove,remove_ifβ function template β removes elements satisfying specific criteria. Like the non-flat_map cousin of this whole vibe.
Okay bestie, that's the whole page, you ate that π π
Source: http://cppreference.com β dated 2024.06.10 β manual page std::erase_...::flat_map)(3). Go forth and erase responsibly, mwah π«Άπ
π¦ 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