std::erase,...std::vector)(3)

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

πŸ’… std::erase, std::erase_if (std::vector) β€” the glow-up πŸ¦‹

okay so bestie, this is a page straight outta the C++ Standard Library manual, and it's about the two little functions that let you just... yeet stuff out of a std::vector. no loop gymnastics, no it = c.erase(it) heartbreak. we love to see it. ✨


🌸 NAME 🌸

std::erase, std::erase_if (for std::vector) β€” the delete-things-from-a-vector duo, std::erase,std::erase_if(std::vector)


✨ SYNOPSIS ✨

okay so first things first, she's Defined in header <vector> πŸ’–

template< class T, class Alloc, class U >
constexpr std::vector<T, Alloc>::size_type
    erase( std::vector<T, Alloc>& c, const U& value );
                                              (since C++20) (until C++26)

template< class T, class Alloc, class U = T >
constexpr std::vector<T, Alloc>::size_type
    erase( std::vector<T, Alloc>& c, const U& value );      (1) (since C++26)

template< class T, class Alloc, class Pred >
constexpr std::vector<T, Alloc>::size_type
    erase_if( std::vector<T, Alloc>& c, Pred pred );        (2) (since C++20)

Now let's break down what these icons actually do πŸ‘‘

  1. erase nukes every element that compares equal to value from the container. it's literally equivalent to this, no cap:
auto it = std::remove(c.begin(), c.end(), value);
auto r = c.end() - it;
c.erase(it, c.end());
return r;
  1. erase_if nukes every element that satisfies the predicate pred. same energy, also literally equivalent to:
auto it = std::remove_if(c.begin(), c.end(), pred);
auto r = c.end() - it;
c.erase(it, c.end());
return r;

πŸŽ€ PARAMETERS πŸŽ€

  • c β€” the container you're about to do surgery on, bestie. this is where the erasing happens.
  • value β€” the value that gets removed. if it matches, it's gone, periodt.
  • pred β€” a unary predicate that returns true if the element should be erased πŸ‘‹
🫢 tiny but important vibe check on pred: the expression pred(v) must be convertible to bool for every argument v of type (possibly const) T, regardless of value category, and it must not modify v. So a parameter type of T& is not allowed, and neither is T unless for T a move is equivalent to a copy (since C++11). yeah. she's strict. it's giving boundaries. πŸ’…

⭐ RETURN VALUE ⭐

the number of erased elements. that's it, that's the tea. (super useful for the "how many did i just delete" flex πŸ“)


🌈 COMPLEXITY 🌈

Linear. lowkey efficient, highkey iconic. ✨


πŸ¦‹ NOTES πŸ¦‹

there's a feature-test macro, and yes we're gonna list her properly:

  • __cpp_lib_algorithm_default_value_type β€” value 202403, std (C++26), feature: List-initialization for algorithm (1)

i know. i know. feature-test macros are the least main-character thing on this page. but we keep receipts. πŸ§ΎπŸ’œ


πŸ”₯ EXAMPLE πŸ”₯

// Run this code

#include <complex>
#include <iostream>
#include <numeric>
#include <string_view>
#include <vector>

void println(std::string_view comment, const auto& c)
{
    std::cout << comment << "{ ";
    for (const auto& x : c)
        std::cout << x << ' ';
    std::cout << "}\n";
}

int main()
{
    std::vector<char> cnt(10);
    std::iota(cnt.begin(), cnt.end(), '0');
    println("Initially, cnt = ", cnt);

    std::erase(cnt, '3');
    println("After erase '3', cnt = ", cnt);

    auto erased = std::erase_if(cnt, [](char x) { return (x - '0') % 2 == 0; });
    println("After erase all even numbers, cnt = ", cnt);
    std::cout << "Erased even numbers: " << erased << '\n';

    std::vector<std::complex<double>> nums{{2, 2}, {4, 2}, {4, 8}, {4, 2}};
    #ifdef __cpp_lib_algorithm_default_value_type
        std::erase(nums, {4, 2});
    #else
        std::erase(nums, std::complex<double>{4, 2});
    #endif
    println("After erase {4, 2}, nums = ", nums);
}

🌟 Output 🌟

Initially, cnt = { 0 1 2 3 4 5 6 7 8 9 }
After erase '3', cnt = { 0 1 2 4 5 6 7 8 9 }
After erase all even numbers, cnt = { 1 5 7 9 }
Erased even numbers: 5
After erase {4, 2}, nums = { (2,2) (4,8) }

not the evens getting absolutely obliterated 😭 the 5 count ate though. 🎯


πŸ‘‘ SEE ALSO πŸ‘‘

  • remove β€” removes elements satisfying specific criteria
  • remove_if β€” (function template) β€” the same situation but with a predicate, iykyk

anyway bestie that's the whole page πŸ’– go forth and erase responsibly. this masterpiece of documentation was served up fresh from http://cppreference.com, dated 2024.06.10 β€” and it's the std::erase,...std::vector)(3) page, so you know exactly where to find her again. mwah πŸ«Άβœ¨πŸŽ€

πŸ“¦ where this page comes from