std::basic_string<CharT,Traits,Allocator>::replace
来自 cppreference.cn
< cpp | string | basic string
basic_string& replace( size_type pos, size_type count, const basic_string& str ); |
(1) | (constexpr since C++20) |
basic_string& replace( const_iterator first, const_iterator last, const basic_string& str ); |
(2) | (constexpr since C++20) |
(3) | ||
basic_string& replace( size_type pos, size_type count, const basic_string& str, |
(until C++14) | |
basic_string& replace( size_type pos, size_type count, const basic_string& str, |
(since C++14) (constexpr since C++20) |
|
basic_string& replace( size_type pos, size_type count, const CharT* cstr, size_type count2 ); |
(4) | (constexpr since C++20) |
basic_string& replace( const_iterator first, const_iterator last, const CharT* cstr, size_type count2 ); |
(5) | (constexpr since C++20) |
basic_string& replace( size_type pos, size_type count, const CharT* cstr ); |
(6) | (constexpr since C++20) |
basic_string& replace( const_iterator first, const_iterator last, const CharT* cstr ); |
(7) | (constexpr since C++20) |
basic_string& replace( size_type pos, size_type count, size_type count2, CharT ch ); |
(8) | (constexpr since C++20) |
basic_string& replace( const_iterator first, const_iterator last, size_type count2, CharT ch ); |
(9) | (constexpr since C++20) |
template< class InputIt > basic_string& replace( const_iterator first, const_iterator last, |
(10) | (constexpr since C++20) |
basic_string& replace( const_iterator first, const_iterator last, std::initializer_list<CharT> ilist ); |
(11) | (since C++11) (constexpr since C++20) |
template< class StringViewLike > basic_string& replace( size_type pos, size_type count, |
(12) | (since C++17) (constexpr since C++20) |
template< class StringViewLike > basic_string& replace( const_iterator first, const_iterator last, |
(13) | (since C++17) (constexpr since C++20) |
template< class StringViewLike > basic_string& replace( size_type pos, size_type count, |
(14) | (since C++17) (constexpr since C++20) |
替换范围 [
begin() + pos,
begin() + std::min(pos + count, size()))
或 [
first,
last)
中的字符为给定的字符。
1,2) 这些字符被替换为 str。
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 中的字符。
这些重载仅在 std::is_convertible_v<const StringViewLike&,
std::basic_string_view<CharT, Traits>> 为 true 且 std::is_convertible_v<const StringViewLike&, const CharT*> 为 false 时参与重载决议。
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 时参与重载决议。
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.
[编辑] 异常
如果操作将导致 size()
超过 max_size()
,则抛出 std::length_error。
如果由于任何原因抛出异常,这些函数不起作用(强异常安全保证)。
[编辑] 示例
本节尚不完整 原因:没有示例 |
[编辑] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 847 | C++98 | 没有异常安全保证 | 添加了强异常安全保证 |
LWG 1323 | C++98 | first 和 last 的类型为 iterator |
更改为 const_iterator |
LWG 2946 | C++17 | 重载 (12,13) 在某些情况下引起歧义 | 通过将它们设为模板来避免 |
[编辑] 参见
(C++23) |
用字符范围替换字符串的指定部分 (公共成员函数) |
(C++11) |
用格式化的替换文本替换正则表达式的出现 (函数模板) |
用另一个值替换所有满足特定条件的值 (函数模板) |