std::erase,...std::vector)(3)
π 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 π
erasenukes every element that compares equal tovaluefrom 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;
erase_ifnukes every element that satisfies the predicatepred. 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 returnstrueif the element should be erased π
π«Ά tiny but important vibe check onpred: the expressionpred(v)must be convertible toboolfor every argumentvof type (possibly const)T, regardless of value category, and it must not modifyv. So a parameter type ofT&is not allowed, and neither isTunless forTa 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β value202403, 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 criteriaremove_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
- 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