std::basic_string<CharT,Traits,Allocator>::replace
(1) | ||
basic_string& replace( size_type pos, size_type count, const basic_string& str ); |
(C++20未満) | |
constexpr basic_string& replace( size_type pos, size_type count, const basic_string& str ); |
(C++20以上) | |
(1) | ||
basic_string& replace( const_iterator first, const_iterator last, const basic_string& str ); |
(C++20未満) | |
constexpr basic_string& replace( const_iterator first, const_iterator last, const basic_string& str ); |
(C++20以上) | |
(2) | ||
basic_string& replace( size_type pos, size_type count, const basic_string& str, |
(C++14未満) | |
basic_string& replace( size_type pos, size_type count, const basic_string& str, |
(C++14以上) (C++20未満) |
|
constexpr basic_string& replace( size_type pos, size_type count, const basic_string& str, |
(C++20以上) | |
(3) | ||
template< class InputIt > basic_string& replace( const_iterator first, const_iterator last, |
(C++20未満) | |
template< class InputIt > constexpr basic_string& replace( const_iterator first, const_iterator last, |
(C++20以上) | |
(4) | ||
basic_string& replace( size_type pos, size_type count, const CharT* cstr, size_type count2 ); |
(C++20未満) | |
constexpr basic_string& replace( size_type pos, size_type count, const CharT* cstr, size_type count2 ); |
(C++20以上) | |
(4) | ||
basic_string& replace( const_iterator first, const_iterator last, const CharT* cstr, size_type count2 ); |
(C++20未満) | |
constexpr basic_string& replace( const_iterator first, const_iterator last, const CharT* cstr, size_type count2 ); |
(C++20以上) | |
(5) | ||
basic_string& replace( size_type pos, size_type count, const CharT* cstr ); |
(C++20未満) | |
constexpr basic_string& replace( size_type pos, size_type count, const CharT* cstr ); |
(C++20以上) | |
(5) | ||
basic_string& replace( const_iterator first, const_iterator last, const CharT* cstr ); |
(C++20未満) | |
constexpr basic_string& replace( const_iterator first, const_iterator last, const CharT* cstr ); |
(C++20以上) | |
(6) | ||
basic_string& replace( size_type pos, size_type count, size_type count2, CharT ch ); |
(C++20未満) | |
constexpr basic_string& replace( size_type pos, size_type count, size_type count2, CharT ch ); |
(C++20以上) | |
(6) | ||
basic_string& replace( const_iterator first, const_iterator last, size_type count2, CharT ch ); |
(C++20未満) | |
constexpr basic_string& replace( const_iterator first, const_iterator last, size_type count2, CharT ch ); |
(C++20以上) | |
(7) | ||
basic_string& replace( const_iterator first, const_iterator last, std::initializer_list<CharT> ilist ); |
(C++11以上) (C++20未満) |
|
constexpr basic_string& replace( const_iterator first, const_iterator last, std::initializer_list<CharT> ilist ); |
(C++20以上) | |
(8) | ||
template < class T > basic_string& replace( size_type pos, size_type count, |
(C++17以上) (C++20未満) |
|
template < class T > constexpr basic_string& replace( size_type pos, size_type count, |
(C++20以上) | |
(8) | ||
template < class T > basic_string& replace( const_iterator first, const_iterator last, |
(C++17以上) (C++20未満) |
|
template < class T > constexpr basic_string& replace( const_iterator first, const_iterator last, |
(C++20以上) | |
(9) | ||
template < class T > basic_string& replace( size_type pos, size_type count, const T& t, |
(C++17以上) (C++20未満) |
|
template < class T > constexpr basic_string& replace( size_type pos, size_type count, const T& t, |
(C++20以上) | |
文字列の [pos, pos + count)
または [first, last)
で示される部分を新しい文字列で置き換えます。
新しい文字列は以下のいずれかです。
str
。str
の部分文字列 [pos2, pos2 + count2)
。 ただし count2==npos の場合、または str.size() を超える場合、 [pos2, str.size())
が使用されます。[first2, last2)
の文字。
|
(C++11未満) |
このオーバーロードは、 |
(C++11以上) |
[cstr, cstr + count2)
の文字。[cstr, cstr + Traits::length(cstr))
の文字。ch
の count2
個のコピー。ilist
内の文字。t
から変換された文字列ビュー sv
の文字。 これらのオーバーロードは、std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> が true であり、 std::is_convertible_v<const T&, const CharT*> が false である場合にのみ、オーバーロード解決に参加しますt
から変換された文字列ビュー sv
のサブビュー [pos2, pos2 + count2)
。 ただし count2==npos の場合、または sv.size() を超える場合、 [pos2, sv.size())
が使用されます。 このオーバーロードは、std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> が true であり、 std::is_convertible_v<const T&, const CharT*> が false である場合にのみ、オーバーロード解決に参加します.目次 |
[編集] 引数
pos | - | 置き換えられる部分文字列の開始位置 |
count | - | 置き換えられる部分文字列の長さ |
first, last | - | 置き換えられる文字の範囲 |
str | - | 置き換えに使用する文字列 |
pos2 | - | 置き換える部分文字列の開始位置 |
count2 | - | 置き換える文字数 |
cstr | - | 置き換えに使用する文字列を指すポインタ |
ch | - | 置き換えに使用する文字値 |
first2, last2 | - | 置き換えに使用する文字の範囲 |
ilist | - | 置き換えに使用する文字を持つ初期化子リスト |
t | - | 置き換えに使用する文字を持つ (std::basic_string_view に変換可能な) オブジェクト |
[編集] 戻り値
*this。
[編集] 例外
pos > length()
または pos2 > str.length()
の場合 std::out_of_range。
結果の文字列が可能な最大文字列長 (max_size()) を超える場合 std::length_error。
いかなるケースでも、何らかの理由で例外が投げられた場合、この関数は効果を持ちません (強い例外保証)。 (C++11以上)
[編集] 欠陥報告
以下の動作変更欠陥報告は以前に発行された C++ 標準に遡って適用されました。
DR | 適用先 | 発行時の動作 | 正しい動作 |
---|---|---|---|
LWG 2946 | C++17 | string_view overload causes ambiguity in some cases
|
avoided by making it a template |
[編集] 例
#include <iostream> #include <string> int main() { std::string str("The quick brown fox jumps over the lazy dog."); str.replace(10, 5, "red"); // (5) str.replace(str.begin(), str.begin() + 3, 1, 'A'); // (6) std::cout << str << '\n'; }
出力:
A quick red fox jumps over the lazy dog.