Namespaces
Variants

std::basic_string<CharT,Traits,Allocator>:: replace

From cppreference.net
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 为字符串视图 sv ,如同通过 std:: basic_string_view < CharT, Traits > sv = t ; 实现,随后将这些字符替换为来自 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 转换为字符串视图 sv ,如同通过 std:: basic_string_view < CharT, Traits > sv = t ; 实现,随后将这些字符替换为子视图 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 ) 不是有效的 范围 ,则行为未定义。

目录

参数

pos - 将被替换子串的起始位置
count - 将被替换子串的长度
first, last - 将被替换的字符范围
str - 用于替换的字符串
pos2 - 替换用子串的起始位置
count2 - 用于替换的字符数量
cstr - 指向用于替换的字符字符串的指针
ch - 用于替换的字符值
first2, last2 - 用于替换的字符范围
ilist - 包含用于替换字符的初始化列表
t - 包含用于替换字符的对象(可转换为 std::basic_string_view
类型要求
-
InputIt 必须满足 LegacyInputIterator 的要求。

返回值

* this

异常

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

若因任何原因抛出异常,这些函数将不产生任何效果( 强异常安全保证 )。

示例

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用标准 发布时行为 正确行为
LWG 847 C++98 未提供异常安全保证 增加强异常安全保证
LWG 1323 C++98 first last 的类型为 iterator 改为 const_iterator
LWG 2946 C++17 重载 (12,13) 在某些情况下会导致歧义 通过将其设为模板来避免

参见

将字符串的指定部分替换为字符范围
(公开成员函数)
使用格式化替换文本替换正则表达式的匹配项
(函数模板)
将满足特定条件的所有值替换为另一个值
(函数模板)