public member function
<string>

std::basic_string::replace

string (1)
basic_string& replace (size_type pos, size_type len, const basic_string& str);basic_string& replace (iterator i1,   iterator i2,   const basic_string& str);
substring (2)
basic_string& replace (size_type pos, size_type len, const basic_string& str,                       size_type subpos, size_type sublen);
c-string (3)
basic_string& replace (size_type pos, size_type len, const charT* s);basic_string& replace (iterator i1,   iterator i2,   const charT* s);
buffer (4)
basic_string& replace (size_type pos, size_type len, const charT* s, size_type n);basic_string& replace (iterator i1,   iterator i2,   const charT* s, size_type n);
fill (5)
basic_string& replace (size_type pos, size_type len, size_type n, charT c);basic_string& replace (iterator i1,   iterator i2,   size_type n, charT c);
range (6)
template <class InputIterator>  basic_string& replace (iterator i1, iterator i2,                       InputIterator first, InputIterator last);
string (1)
basic_string& replace (size_type pos,     size_type len,     const basic_string& str);basic_string& replace (const_iterator i1, const_iterator i2, const basic_string& str);
substring (2)
basic_string& replace (size_type pos, size_type len, const basic_string& str,                       size_type subpos, size_type sublen);
c-string (3)
basic_string& replace (size_type pos,     size_type len,     const charT* s);basic_string& replace (const_iterator i1, const_iterator i2, const charT* s);
buffer (4)
basic_string& replace (size_type pos,     size_type len,     const charT* s, size_type n);basic_string& replace (const_iterator i1, const_iterator i2, const charT* s, size_type n);
fill (5)
basic_string& replace (size_type pos,     size_type len,     size_type n, charT c);basic_string& replace (const_iterator i1, const_iterator i2, size_type n, charT c);
range (6)
template <class InputIterator>  basic_string& replace (const_iterator i1, const_iterator i2,                       InputIterator first, InputIterator last);
初始化列表 (7)
basic_string& replace (const_iterator i1, const_iterator i2, initializer_list<charT> il);
string (1)
basic_string& replace (size_type pos,     size_type len,     const basic_string& str);basic_string& replace (const_iterator i1, const_iterator i2, const basic_string& str);
substring (2)
basic_string& replace (size_type pos, size_type len, const basic_string& str,                       size_type subpos, size_type sublen = npos);
c-string (3)
basic_string& replace (size_type pos,     size_type len,     const charT* s);basic_string& replace (const_iterator i1, const_iterator i2, const charT* s);
buffer (4)
basic_string& replace (size_type pos,     size_type len,     const charT* s, size_type n);basic_string& replace (const_iterator i1, const_iterator i2, const charT* s, size_type n);
fill (5)
basic_string& replace (size_type pos,     size_type len,     size_type n, charT c);basic_string& replace (const_iterator i1, const_iterator i2, size_type n, charT c);
range (6)
template <class InputIterator>  basic_string& replace (const_iterator i1, const_iterator i2,                       InputIterator first, InputIterator last);
初始化列表 (7)
basic_string& replace (const_iterator i1, const_iterator i2, initializer_list<charT> il);
替換字串的部分內容
替換從字元pos開始並跨越len個字元的字串部分(或者[i1,i2)) 之間範圍內的字串部分)為新的內容。

(1) string
複製 *str*。
(2) substring
複製str中從字元位置subpos開始並跨越sublen個字元的部分(或者直到str的末尾,如果str太短或者sublenbasic_string::npos)。
(3) c-string
複製 *s* 指向的空終止字元序列(C 字串)。
長度透過呼叫traits.length(s) 確定。.
(4) buffer
從 *s* 指向的字元陣列中複製前 *n* 個字元。
(5) fill
將字串的部分內容替換為n個字元c的連續副本。
(6) range
複製範圍內的字元序列[first,last),以相同的順序。
(7) 初始化列表
按相同順序複製 *il* 中的每個字元。

引數

str
另一個具有相同型別的basic_string物件(具有相同的類模板引數charT, 特性 (traits)Alloc),其值被複制。
pos
要替換的第一個字元的位置。
如果它大於字串長度,則丟擲out_of_range
len
要替換的字元數(如果字串較短,則會替換儘可能多的字元)。
basic_string::npos值表示直到字串末尾的所有字元。
subpos
str中作為替換複製到物件的第一個字元的位置。
如果它大於str長度,則丟擲out_of_range
sublen
要複製的子字串的長度(如果字串較短,則會複製儘可能多的字元)。
basic_string::npos值表示直到str末尾的所有字元。
s
指向字元陣列的指標(例如c-string)。
n
要複製的字元數。
c
字元值,重複 *n* 次。
first, last
指向範圍內初始位置和最終位置的輸入迭代器。 使用的範圍是[first,last),其中包括firstlast之間的所有字元,包括first指向的字元,但不包括last指向的字元。
函式模板引數InputIterator應為指向可轉換為charT.
il
il
這些物件是從初始化列表宣告符自動構造的。

charTbasic_string 的字元型別(即,它的第一個模板引數)。
成員型別size_type是一種無符號整型型別。

返回值

*this

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// replacing in a string
#include <iostream>
#include <string>

int main ()
{
  std::string base="this is a test string.";
  std::string str2="n example";
  std::string str3="sample phrase";
  std::string str4="useful.";

  // replace signatures used in the same order as described above:

  // Using positions:                 0123456789*123456789*12345
  std::string str=base;           // "this is a test string."
  str.replace(9,5,str2);          // "this is an example string." (1)
  str.replace(19,6,str3,7,6);     // "this is an example phrase." (2)
  str.replace(8,10,"just a");     // "this is just a phrase."     (3)
  str.replace(8,6,"a shorty",7);  // "this is a short phrase."    (4)
  str.replace(22,1,3,'!');        // "this is a short phrase!!!"  (5)

  // Using iterators:                                               0123456789*123456789*
  str.replace(str.begin(),str.end()-3,str3);                    // "sample phrase!!!"      (1)
  str.replace(str.begin(),str.begin()+6,"replace");             // "replace phrase!!!"     (3)
  str.replace(str.begin()+8,str.begin()+14,"is coolness",7);    // "replace is cool!!!"    (4)
  str.replace(str.begin()+12,str.end()-4,4,'o');                // "replace is cooool!!!"  (5)
  str.replace(str.begin()+11,str.end(),str4.begin(),str4.end());// "replace is useful."    (6)
  std::cout << str << '\n';
  return 0;
}

輸出
replace is useful.


複雜度

未指定,但通常高達新字串長度的線性。

迭代器有效性

與此物件相關的任何迭代器、指標和引用都可能失效。

資料競爭

物件被修改。

異常安全

強保證: 如果丟擲異常,則 basic_string 中沒有任何更改。

如果已知s沒有指向足夠長的陣列,或者如果指定的範圍是[first,last)無效,則會導致未定義行為

如果pos大於字串長度,或者subpos大於str長度,則會丟擲out_of_range異常。
如果結果 字串長度 超過 max_size,則會丟擲 length_error 異常。
如果該型別使用預設分配器,如果函式需要分配儲存空間但失敗,則會丟擲bad_alloc異常。

另見