Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |string‎ |basic string
       
       
       
      std::basic_string
       
      basic_string& replace( size_type pos, size_type count,
                             const basic_string& str);
      (1)(constexpr since C++20)
      basic_string& replace( const_iterator first, const_iterator last,
                             const basic_string& str);
      (2)(constexpr since C++20)
      (3)
      basic_string& replace( size_type pos, size_type count,

                             const basic_string& str,

                             size_type pos2, size_type count2);
      (until C++14)
      basic_string& replace( size_type pos, size_type count,

                             const basic_string& str,

                             size_type pos2, size_type count2= npos);
      (since C++14)
      (constexpr since C++20)
      basic_string& replace( size_type pos, size_type count,
                             const CharT* cstr, size_type count2);
      (4)(constexpr since C++20)
      basic_string& replace( const_iterator first, const_iterator last,
                             const CharT* cstr, size_type count2);
      (5)(constexpr since C++20)
      basic_string& replace( size_type pos, size_type count,
                             const CharT* cstr);
      (6)(constexpr since C++20)
      basic_string& replace( const_iterator first, const_iterator last,
                             const CharT* cstr);
      (7)(constexpr since C++20)
      basic_string& replace( size_type pos, size_type count,
                             size_type count2, CharT ch);
      (8)(constexpr since C++20)
      basic_string& replace( const_iterator first, const_iterator last,
                             size_type count2, CharT ch);
      (9)(constexpr since C++20)
      template<class InputIt>

      basic_string& replace( const_iterator first, const_iterator last,

                             InputIt first2, InputIt last2);
      (10)(constexpr since C++20)
      basic_string& replace( const_iterator first, const_iterator last,
                             std::initializer_list<CharT> ilist);
      (11)(since C++11)
      (constexpr since C++20)
      template<class StringViewLike>

      basic_string& replace( size_type pos, size_type count,

                             const StringViewLike& t);
      (12)(since C++17)
      (constexpr since C++20)
      template<class StringViewLike>

      basic_string& replace( const_iterator first, const_iterator last,

                             const StringViewLike& t);
      (13)(since C++17)
      (constexpr since C++20)
      template<class StringViewLike>

      basic_string& replace( size_type pos, size_type count,
                             const StringViewLike& t,

                             size_type pos2, size_type count2= npos);
      (14)(since C++17)
      (constexpr since C++20)

      Replaces the characters in the range[begin()+ posbegin()+std::min(pos+ count, size())) or[firstlast) with given characters.

      1,2) Those characters are replaced withstr.
      3) Those characters are replaced with a substring[pos2std::min(pos2+ count2, str.size())) ofstr.
      4,5) Those characters are replaced with the characters in the range[cstrcstr+ count2).
      If[cstrcstr+ count2) is not avalid range, the behavior is undefined.
      6,7) Those characters are replaced with the characters in the range[cstrcstr+ Traits::length(cstr)).
      8,9) Those characters are replaced withcount2 copies ofch.
      10) Those characters are replaced with the characters in the range[first2last2) as if byreplace(first, last, basic_string(first2, last2, get_allocator())).
      11) Those characters are replaced with the characters inilist.
      12,13) Implicitly convertst to a string viewsv as if bystd::basic_string_view<CharT, Traits> sv= t;, then those characters are replaced with the characters fromsv.
      These overloads participate in overload resolution only ifstd::is_convertible_v<const StringViewLike&,
                           std::basic_string_view<CharT, Traits>>
      istrue andstd::is_convertible_v<const StringViewLike&,const CharT*> isfalse.
      14) Implicitly convertst to a string viewsv as if bystd::basic_string_view<CharT, Traits> sv= t;, then those characters are replaced with the characters from the subviewsv.substr(pos2, count2).
      This overload participates in overload resolution only ifstd::is_convertible_v<const StringViewLike&,
                           std::basic_string_view<CharT, Traits>>
      istrue andstd::is_convertible_v<const StringViewLike&,const CharT*> isfalse.

      If[begin()first) or[firstlast) is not avalid range, the behavior is undefined.

      Contents

      [edit]Parameters

      pos - start of the substring that is going to be replaced
      count - length of the substring that is going to be replaced
      first, last - range of characters that is going to be replaced
      str - string to use for replacement
      pos2 - start of the substring to replace with
      count2 - number of characters to replace with
      cstr - pointer to the character string to use for replacement
      ch - character value to use for replacement
      first2, last2 - range of characters to use for replacement
      ilist - initializer list with the characters to use for replacement
      t - object (convertible tostd::basic_string_view) with the characters to use for replacement
      Type requirements
      -
      InputIt must meet the requirements ofLegacyInputIterator.

      [edit]Return value

      *this.

      [edit]Exceptions

      1) Throwsstd::out_of_range ifpos> size().
      3) Throwsstd::out_of_range ifpos> size() orpos2> str.size().
      4,6,8) Throwsstd::out_of_range ifpos> size().
      12,14) Throwsstd::out_of_range ifpos> size().

      If the operation would causesize() to exceedmax_size(), throwsstd::length_error.

      If an exception is thrown for any reason, these functions have no effect (strong exception safety guarantee).

      [edit]Example

      This section is incomplete
      Reason: no example

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 847C++98there was no exception safety guaranteeadded strong exception safety guarantee
      LWG 1323C++98the types offirst andlast wereiteratorchanged toconst_iterator
      LWG 2946C++17overloads(12,13) caused ambiguity in some casesavoided by making them templates

      [edit]See also

      replaces specified portion of a string with a range of characters
      (public member function)[edit]
      replaces occurrences of a regular expression with formatted replacement text
      (function template)[edit]
      replaces all values satisfying specific criteria with another value
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/replace&oldid=156446"

      [8]ページ先頭

      ©2009-2025 Movatter.jp