|
|
|
Defined in header <string> | ||
(1) | ||
template<class CharT,class Traits,class Alloc,class U> constexprtypenamestd::basic_string<CharT, Traits, Alloc>::size_type | (since C++20) (until C++26) | |
template<class CharT,class Traits,class Alloc,class U= CharT> constexprtypenamestd::basic_string<CharT, Traits, Alloc>::size_type | (since C++26) | |
template<class CharT,class Traits,class Alloc,class Pred> constexprtypenamestd::basic_string<CharT, Traits, Alloc>::size_type | (2) | (since C++20) |
Contents |
c | - | container from which to erase |
value | - | value to be removed |
pred | - | unary predicate which returns true if the element should be erased. The expressionpred(v) must be convertible tobool for every argument |
The number of erased elements.
Linear.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_algorithm_default_value_type | 202403 | (C++26) | List-initialization forstd::erase |
#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'; #if __cpp_lib_algorithm_default_value_typestd::erase(word,{'g'});std::cout<<"After erase {'g'}: "<<std::quoted(word)<<'\n';#endif}
Possible output:
Initially, word = "startling"After erase 'l', word = "starting"After erase all 'a', 'r', and 't': "sing"Erased symbols count: 4After erase {'g'}: "sin"
removes elements satisfying specific criteria (function template)[edit] | |
(C++20)(C++20) | removes elements satisfying specific criteria (algorithm function object)[edit] |