std::erase_...::flat_map)(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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: Key and T must 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 returns true if the element should be erased. Basically the bouncer at the door: true means 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