Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_string<CharT,Traits,Allocator>::erase

      From cppreference.com
      <cpp‎ |string‎ |basic string
       
       
       
      std::basic_string
       
      basic_string& erase( size_type index=0, size_type count= npos);
      (1)(constexpr since C++20)
      (2)
      iterator erase( iterator position);
      (until C++11)
      iterator erase( const_iterator position);
      (since C++11)
      (constexpr since C++20)
      (3)
      iterator erase( iterator first, iterator last);
      (until C++11)
      iterator erase( const_iterator first, const_iterator last);
      (since C++11)
      (constexpr since C++20)

      Removes specified characters from the string.

      1) Removesstd::min(count, size()- index) characters starting atindex.
      2) Removes the character atposition.
      Ifposition is not adereferenceable iterator on*this, the behavior is undefined.
      3) Removes the characters in the range[firstlast).
      Iffirst orlast is not avalid iterator on*this, or[firstlast) is not avalid range, the behavior is undefined.

      Contents

      [edit]Parameters

      index - first character to remove
      count - number of characters to remove
      position - iterator to the character to remove
      first, last - range of the characters to remove

      [edit]Return value

      1)*this
      2) Iterator pointing to the character immediately following the character erased, orend() if no such character exists.
      3) Iterator pointing to the characterlast pointed to before the erase, orend() if no such character exists.

      [edit]Exceptions

      1)std::out_of_range ifindex> size().
      2,3) Throws nothing.

      If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee).

      [edit]Example

      Run this code
      #include <algorithm>#include <iostream>#include <iterator>#include <string> int main(){std::string s="This Is An Example";std::cout<<"1) "<< s<<'\n';     s.erase(7,3);// erases " An" using overload (1)std::cout<<"2) "<< s<<'\n';     s.erase(std::find(s.begin(), s.end(),' '));// erases first ' '; overload (2)std::cout<<"3) "<< s<<'\n';     s.erase(s.find(' '));// trims from ' ' to the end of the string; overload (1)std::cout<<"4) "<< s<<'\n'; auto it=std::next(s.begin(), s.find('s'));// obtains iterator to the first 's'    s.erase(it,std::next(it,2));// erases "sI"; overload (3)std::cout<<"5) "<< s<<'\n';}

      Output:

      1) This Is An Example2) This Is Example3) ThisIs Example4) ThisIs5) This

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 27C++98overload(3) did not erase the characterlast pointed to, but it returned
      the iterator pointing to the character immediately following that character
      returns an iterator
      pointing to that character
      LWG 428C++98overload(2) explicitly requiredposition to be valid, but
      SequenceContainer requires it to be dereferenceable (stricter)
      removed the
      explicit requirement
      LWG 847C++98there was no exception safety guaranteeadded strong exception
      safety guarantee

      [edit]See also

      clears the contents
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/erase&oldid=170792"

      [8]ページ先頭

      ©2009-2025 Movatter.jp