名前空間
変種
操作

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) (C++20 以降 constexpr)
basic_string& replace( const_iterator first, const_iterator last,
                       const basic_string& str );
(2) (C++20 以降 constexpr)
(3)
basic_string& replace( size_type pos, size_type count,

                       const basic_string& str,

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

                       const basic_string& str,

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

basic_string& replace( const_iterator first, const_iterator last,

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

basic_string& replace( size_type pos, size_type count,

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

basic_string& replace( const_iterator first, const_iterator last,

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

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

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

範囲 [ begin() + pos , begin() + std::min(pos + count, size()) ) または [ first , last ) の文字を指定された文字で置換します。

1,2) これらの文字は str で置換されます。
3) これらの文字は str の部分文字列 [ pos2 , std::min(pos2 + count2, str.size()) ) で置換されます。
4,5) これらの文字は範囲 [ cstr , cstr + count2 ) の文字で置換されます。
範囲 [ cstr , cstr + count2 ) が 有効な範囲でない場合、動作は未定義です。
6,7) これらの文字は範囲 [ cstr , cstr + Traits::length(cstr) ) の文字で置換されます。
8,9) これらの文字は count2 個の ch のコピーで置換されます。
10) これらの文字は範囲 [ first2 , last2 ) の文字で、replace(first, last, basic_string(first2, last2, get_allocator())) のように置換されます。
11) これらの文字は ilist の文字で置換されます。
12,13) t は、std::basic_string_view<CharT, Traits> sv = t; のように文字列ビュー sv に暗黙的に変換され、これらの文字は sv の文字で置換されます。
これらのオーバーロードは、std::is_convertible_v<const StringViewLike&,
                      std::basic_string_view<CharT, Traits>>
true であり、かつ std::is_convertible_v<const StringViewLike&, const CharT*>false である場合にのみ、オーバーロード解決に参加します。
14) t は、std::basic_string_view<CharT, Traits> sv = t; のように文字列ビュー sv に暗黙的に変換され、これらの文字はビュー sv.substr(pos2, count2) の文字で置換されます。
このオーバーロードは、std::is_convertible_v<const StringViewLike&,
                      std::basic_string_view<CharT, Traits>>
true であり、かつ std::is_convertible_v<const StringViewLike&, const CharT*>false である場合にのみ、オーバーロード解決に参加します。

範囲 [ begin() , first ) または [ first , last ) が 有効な範囲でない場合、動作は未定義です。

目次

[edit] Parameters

pos - 置換される部分文字列の開始位置
count - 置換される部分文字列の長さ
first, last - 置換される文字の範囲
str - 置換に使用する文字列
pos2 - 置換する部分文字列の開始位置
count2 - 置換する文字数
cstr - 置換に使用する文字文字列へのポインタ
文字 - 置換に使用する文字の値
first2, last2 - 置換する文字の範囲
ilist - 置換に使用する文字を含む初期化リスト
t - 置換に使用する文字を含むオブジェクト(std::basic_string_view に変換可能)
型要件
-
InputItLegacyInputIterator の要件を満たす必要があります。

[edit] Return value

*this.

[edit] Exceptions

1) pos > size() の場合、std::out_of_range をスローします。
3) pos > size() または pos2 > str.size() の場合、std::out_of_range をスローします。
4,6,8) pos > size() の場合、std::out_of_range をスローします。
12,14) pos > size() の場合、std::out_of_range をスローします。

操作によりsize()max_size()を超える場合、std::length_errorを送出します。

何らかの理由で例外がスローされた場合、これらの関数は効果がありません(強力な例外安全性保証)。

[edit] Example

[edit] Defect reports

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 847 C++98 例外安全性保証がなかった 強力な例外安全性保証を追加
LWG 1323 C++98 first および last の型が iterator であった const_iterator に変更された
LWG 2946 C++17 オーバーロード (12,13) が一部のケースで曖昧さを引き起こした テンプレート化することで回避された

[edit] See also

指定された文字列の一部を文字の範囲で置換します
(public member function) [edit]
正規表現に一致した箇所を、書式化された置換テキストで置き換える
(関数テンプレート) [編集]
特定の基準を満たすすべての値を別の値に置き換える
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)