The Wayback Machine - https://web.archive.org/web/20180418162755/http://ja.cppreference.com:80/w/cpp/string/basic_string/replace
名前空間
変種
操作

std::basic_string::replace

提供: cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
basic_string& replace( size_type pos, size_type count,
                       const basic_string& str );
(1)
basic_string& replace( const_iterator first, const_iterator last,
                       const basic_string& str );
(1)
(2)
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およびそれ以降)
template<class InputIt >

basic_string& replace( const_iterator first, const_iterator last,

                       InputIt first2, InputIt last2 );
(3)
basic_string& replace( size_type pos, size_type count,
                       const CharT* cstr, size_type count2 );
(4)
basic_string& replace( const_iterator first, const_iterator last,
                       const CharT* cstr, size_type count2 );
(4)
basic_string& replace( size_type pos, size_type count,
                       const CharT* cstr );
(5)
basic_string& replace( const_iterator first, const_iterator last,
                       const CharT* cstr );
(5)
basic_string& replace( size_type pos, size_type count,
                       size_type count2, CharT ch );
(6)
basic_string& replace( const_iterator first, const_iterator last,
                       size_type count2, CharT ch );
(6)
basic_string& replace( const_iterator first, const_iterator last,
                       std::initializer_list<CharT> ilist );
(7) (C++11およびそれ以降)
template<class T >

basic_string& replace( size_type pos, size_type count,

                       const T& t );
(8) (C++17およびそれ以降)
template<class T >

basic_string& replace( const_iterator first, const_iterator last,

                       const T& t );
(8) (C++17およびそれ以降)
template<class T >

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

                       size_type pos2, size_type count2 = npos );
(9) (C++17およびそれ以降)

文字列の [pos, pos + count) または [first, last) で示される部分を新しい文字列で置き換えます。

新しい文字列は以下のいずれかです。

1) 文字列 str
2)str の部分文字列 [pos2, pos2 + count2)。 ただし count2==npos の場合、または str.size() を超える場合、 [pos2, str.size()) が使用されます。
3) 範囲 [first2, last2) の文字。 InputIt が整数型の場合、このオーバーロードはオーバーロード (6) と同じ効果を持ちます。
4)cstr の指す文字列の最初の count2 個の文字。
5)cstr の指すNUL終端文字列。
6)chcount2 個のコピー。
7) 初期化子リスト ilist 内の文字。
8)std::basic_string_view<CharT, Traits> sv = t; によって行われたかのように t から変換された文字列ビュー sv の文字。 これらのオーバーロードは、std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>>true であり、 std::is_convertible_v<const T&, const CharT*>false である場合にのみ、オーバーロード解決に参加します
9)std::basic_string_view<CharT, Traits> sv = t; によって行われたかのように 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

結果の文字列が可能な最大文字列長 (std::string::npos - 1) を超える場合 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.
close