basic_string& replace( size_type pos, size_type count, 
                       const basic_string& str ); 
 
basic_string& replace( const_iterator first, const_iterator last, 
 
                       const basic_string& str );  | 
 (1)  | 
   | 
 basic_string& replace( size_type pos, size_type count, 
                       const basic_string& str, 
                       size_type pos2, size_type count2 ); 
 
template< class InputIt > 
basic_string& replace( const_iterator first, const_iterator last, 
 
                       InputIt first2, InputIt last2 );  | 
 (2)  | 
   | 
 basic_string& replace( size_type pos, size_type count, 
                       const CharT* cstr, size_type count2 ); 
 
basic_string& replace( const_iterator first, const_iterator last, 
 
                       const CharT* cstr, size_type count2 );  | 
 (3)  | 
   | 
 basic_string& replace( size_type pos, size_type count, 
                       const CharT* cstr ); 
 
basic_string& replace( const_iterator first, const_iterator last, 
 
                       const CharT* cstr );  | 
 (4)  | 
   | 
 basic_string& replace( size_type pos, size_type count, 
                       size_type count2, CharT ch ); 
 
basic_string& replace( const_iterator first, const_iterator last, 
 
                       size_type count2, CharT ch );  | 
 (5)  | 
   | 
|   | 
 (6)  | 
 (C++11 起)  | 
 |  |  | 
无论是
[pos, pos + count)或
[first, last)一个新的字符串表示的字符串替换的部分.
原文:
Replaces the part of the string indicated by either [pos, pos + count) or [first, last) with a new string.
文本通过
谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击
此处查看指示。
 
新的字符串可以是:1
原文:
The new string can be one of:
文本通过
谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击
此处查看指示。
1) 
串
str原文:
string str
文本通过
谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击
此处查看指示。
2) 
子串
[pos2, pos2 + count2)str或字符的范围内
[first2, last2)原文:
substring [pos2, pos2 + count2) of str or characters in the range [first2, last2)
文本通过
谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击
此处查看指示。
3) 
count2charcters指向的字符串的第一
cstr原文:
first count2 charcters of the character string pointed to by cstr
文本通过
谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击
此处查看指示。
4) 
空字符结尾的字符串所指向的
cstr原文:
null-terminated character string pointed to by cstr
文本通过
谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击
此处查看指示。
5) 
count2份的性格
ch原文:
count2 copies of character ch
文本通过
谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击
此处查看指示。
6) 
在初始化列表中
ilist的字符数
原文:
characters in the initializer list ilist
文本通过
谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击
此处查看指示。
 
[编辑] 参数
|   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 文本通过 谷歌翻译机器翻译。 你可以帮忙校正和验证翻译。点击 此处查看指示。  
 | 
|   init
 | 
 -
 | 
  初始化列表中要使用的字符替换 原文:  initializer list with the characters to use for replacement 文本通过 谷歌翻译机器翻译。 你可以帮忙校正和验证翻译。点击 此处查看指示。  
 | 
|  类型要求
 | 
 -InputIt 必须满足  InputIterator 的要求。
 | 
[编辑] 返回值
*this
[编辑] 例外
std::out_of_range if pos > length() or pos2 > str.length()
std::length_error如果结果字符串超过最大可能的字符串长度(
std::string::npos - 1)
原文:
std::length_error if the resulting string will exceed maximum possible string length (std::string::npos - 1)
文本通过
谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击
此处查看指示。
 
[编辑] 示例
#include <iostream>
#include <string>
 
int main()
{
    std::string str("The quick brown fox jumps over the lazy dog.");
 
    str.replace(10, 5, "red"); // (4)
 
    str.replace(str.begin(), str.begin() + 3, 1, 'A'); // (5)
 
    std::cout << str << '\n';
} 
输出:
A quick red fox jumps over the lazy dog.