Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::erase,std::erase_if(std::basic_string)

      From cppreference.com
      <cpp‎ |string‎ |basic string

      [edit template]
       
       
       
      std::basic_string
      Constants
      Non-member functions
      erase(std::basic_string)erase_if(std::basic_string)
      (C++20)(C++20)
      I/O
      Comparison
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
      Numeric conversions
      (C++11)(C++11)(C++11)
      (C++11)(C++11) 
      (C++11)(C++11)(C++11)
      (C++11)
      (C++11)
      Literals
      Helper classes
      Deduction guides(C++17)
       
      Defined in header<string>
      (1)
      template<class CharT,class Traits,class Alloc,class U>

      constexprtypenamestd::basic_string<CharT, Traits, Alloc>::size_type

          erase(std::basic_string<CharT, Traits, Alloc>& c,const U& value);
      (since C++20)
      (until C++26)
      template<class CharT,class Traits,class Alloc,class U= CharT>

      constexprtypenamestd::basic_string<CharT, Traits, Alloc>::size_type

          erase(std::basic_string<CharT, Traits, Alloc>& c,const U& value);
      (since C++26)
      template<class CharT,class Traits,class Alloc,class Pred>

      constexprtypenamestd::basic_string<CharT, Traits, Alloc>::size_type

          erase_if(std::basic_string<CharT, Traits, Alloc>& c, Pred pred);
      (2)(since C++20)
      1) Erases all elements that compare equal tovalue from the containerc. Equivalent toauto 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 predicatepred from the containerc. Equivalent toauto it=std::remove_if(c.begin(), c.end(), pred);
      auto r= c.end()- it;
      c.erase(it, c.end());
      return r;
      .

      Contents

      [edit]Parameters

      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 argumentv of type (possibly const)CharT, regardless ofvalue category, and must not modifyv. Thus, a parameter type ofCharT&is not allowed, nor isCharT unless forCharT a move is equivalent to a copy(since C++11).​

      [edit]Return value

      The number of erased elements.

      [edit]Complexity

      Linear.

      Notes

      Feature-test macroValueStdFeature
      __cpp_lib_algorithm_default_value_type202403(C++26)List-initialization forstd::erase

      [edit]Example

      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'; #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"

      [edit]See also

      removes elements satisfying specific criteria
      (function template)[edit]
      removes elements satisfying specific criteria
      (algorithm function object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/erase2&oldid=121363"

      [8]ページ先頭

      ©2009-2025 Movatter.jp