std::erase,...asic_string)(3)
π std::erase,...asic_string)(3) β C++ Standard Libary π
okay bestie, this is the page for std::erase,std::erase_if(std::basic_string) from the C++ Standard Libary, and itβs literally the βdelete the things you donβt want from a stringβ duo. no cap, the vibes are useful. π
π NAME π
std::erase,std::erase_if(std::basic_string) β yup, thatβs the name twice, bestie: std::erase,std::erase_if(std::basic_string). iconic behavior, honestly. π«Ά
β¨ Synopsis β¨
Defined in header <string> β write that down. π
template< class CharT, class Traits, class Alloc, class U >
constexpr std::basic_string<CharT, Traits, Alloc>::size_type
(since C++20)
(until
C++26)
erase( std::basic_string<CharT, Traits, Alloc>& c, const
U& value );
template< class CharT, class Traits, class Alloc, class U =
CharT >
constexpr std::basic_string<CharT, Traits, Alloc>::size_type (1)
(since C++26)
erase( std::basic_string<CharT, Traits, Alloc>& c, const
U& value );
template< class CharT, class Traits, class Alloc, class Pred >
constexpr std::basic_string<CharT, Traits, Alloc>::size_type
(2) (since C++20)
erase_if( std::basic_string<CharT, Traits, Alloc>& c, Pred
pred );
1) Erases all elements that compare equal to value from the container. Equivalent to
auto it = std::remove(c.begin(), c.end(), value);
auto r = c.end() - it;
c.erase(it, c.end());
return r;
2) Erases all elements that satisfy the predicate pred from the container. 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β container from which to erase.valueβ value to be removed.predβ unary predicate which returnstrueif the element should be erased.- The expression
pred(v)must be convertible toboolfor every argumentvof type (possiblyconst)CharT, regardless of value category, and must not modifyv. - Thus, a parameter type of
CharT&is not allowed, nor isCharTunless forCharTa move is equivalent to a copy (since C++11).
- The expression
This is the part where the compiler is like βbestie, read the predicate rules,β so donβt try to sneak in a CharT& unless you want chaos. π
π Return value π
The number of erased elements. Thatβs it β itβs literally counting the deleted girlies. π
π¦ Complexity π¦
Linear. Periodt. β¨
π§ Notes π§
Hereβs the feature-test macro tea:
- Feature-test macro:
__cpp_lib_algorithm_default_value_type- Value:
202403 - Std:
(C++26) - Feature: List-initialization for algorithm (1)
- Value:
π Example π
Run this code, bestie:
// Run this code
#include <iomanip>
#include <iostream>
#include <string>
int main()
{
std::string word{"startling"};
std::cout << "Initially, word = " << std::quoted(word) << '\n';
std::erase(word, 'l');
std::cout << "After erase 'l': " << std::quoted(word) << '\n';
auto erased = std::erase_if(word, [](char x)
{
return x == 'a' or x == 'r' or x == 't';
});
std::cout << "After erase all 'a', 'r', and 't': " <<
std::quoted(word) << '\n';
std::cout << "Erased symbols count: " << erased << '\n';
}
πΈ Output πΈ
Initially, word = "startling"
After erase 'l', word = "starting"
After erase all 'a', 'r', and 't': "sing"
Erased symbols count: 4
π See also π
removeβ removes elements satisfying specific criteria.remove_ifβ function template.
π Thatβs the tea! Source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::erase,...asic_string)(3) π
π¦ 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