命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | string‎ | basic_string
 
 
 
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 );
(until 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() + posstd::min(pos + count, size()))[firstlast) 中的字符,替换为给定字符。

1,2) 这些字符被 str 替换。
3) 这些字符被 str 的子字符串 [pos2std::min(pos2 + count2, str.size())) 替换。
4,5) 这些字符被范围 [cstrcstr + count2) 中的字符替换。
如果 [cstrcstr + count2) 不是 有效范围,则行为未定义。
6,7) 这些字符被范围 [cstrcstr + Traits::length(cstr)) 中的字符替换。
8,9) 这些字符被 count2ch 替换。
10) 这些字符被范围 [first2last2) 中的字符替换,如同通过 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>>
truestd::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>>
truestd::is_convertible_v<const StringViewLike&, const CharT*>false 时,此重载才参与重载决议。

如果 [begin()first)[firstlast) 不是 有效范围,则行为未定义。

目录

[edit] 参数

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

[edit] 返回值

*this.

[edit] 异常

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

如果因任何原因抛出异常,这些函数没有效果(强异常安全保证)。

[edit] 示例

[edit] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 847 C++98 没有异常安全保证 添加了强异常安全保证
LWG 1323 C++98 firstlast 的类型是 iterator 改为 const_iterator
LWG 2946 C++17 重载 (12,13) 在某些情况下会导致歧义 通过将其变为模板来避免

[edit] 参阅

用字符范围替换字符串的指定部分
(公共成员函数) [编辑]
用格式化的替换文本替换正则表达式的出现
(函数模板) [编辑]
用另一个值替换所有满足特定条件的值
(函数模板) [编辑]