std::basic_string<CharT,Traits,Allocator>::replace
| (1) | ||
| basic_string& replace( size_type pos, size_type count, const basic_string& str ); |
(C++20 前) | |
| constexpr basic_string& replace( size_type pos, size_type count, const basic_string& str ); |
(C++20 起) | |
| (1) | ||
| basic_string& replace( const_iterator first, const_iterator last, const basic_string& str ); |
(C++20 前) | |
| constexpr basic_string& replace( const_iterator first, const_iterator last, const basic_string& str ); |
(C++20 起) | |
| (2) | ||
| basic_string& replace( size_type pos, size_type count, const basic_string& str, |
(C++14 前) | |
| basic_string& replace( size_type pos, size_type count, const basic_string& str, |
(C++14 起) (C++20 前) |
|
| constexpr basic_string& replace( size_type pos, size_type count, const basic_string& str, |
(C++20 起) | |
| (3) | ||
| template< class InputIt > basic_string& replace( const_iterator first, const_iterator last, |
(C++20 前) | |
| template< class InputIt > constexpr basic_string& replace( const_iterator first, const_iterator last, |
(C++20 起) | |
| (4) | ||
| basic_string& replace( size_type pos, size_type count, const CharT* cstr, size_type count2 ); |
(C++20 前) | |
| constexpr basic_string& replace( size_type pos, size_type count, const CharT* cstr, size_type count2 ); |
(C++20 起) | |
| (4) | ||
| basic_string& replace( const_iterator first, const_iterator last, const CharT* cstr, size_type count2 ); |
(C++20 前) | |
| constexpr basic_string& replace( const_iterator first, const_iterator last, const CharT* cstr, size_type count2 ); |
(C++20 起) | |
| (5) | ||
| basic_string& replace( size_type pos, size_type count, const CharT* cstr ); |
(C++20 前) | |
| constexpr basic_string& replace( size_type pos, size_type count, const CharT* cstr ); |
(C++20 起) | |
| (5) | ||
| basic_string& replace( const_iterator first, const_iterator last, const CharT* cstr ); |
(C++20 前) | |
| constexpr basic_string& replace( const_iterator first, const_iterator last, const CharT* cstr ); |
(C++20 起) | |
| (6) | ||
| basic_string& replace( size_type pos, size_type count, size_type count2, CharT ch ); |
(C++20 前) | |
| constexpr basic_string& replace( size_type pos, size_type count, size_type count2, CharT ch ); |
(C++20 起) | |
| (6) | ||
| basic_string& replace( const_iterator first, const_iterator last, size_type count2, CharT ch ); |
(C++20 前) | |
| constexpr basic_string& replace( const_iterator first, const_iterator last, size_type count2, CharT ch ); |
(C++20 起) | |
| (7) | ||
| basic_string& replace( const_iterator first, const_iterator last, std::initializer_list<CharT> ilist ); |
(C++11 起) (C++20 前) |
|
| constexpr basic_string& replace( const_iterator first, const_iterator last, std::initializer_list<CharT> ilist ); |
(C++20 起) | |
| (8) | ||
| template < class T > basic_string& replace( size_type pos, size_type count, |
(C++17 起) (C++20 前) |
|
| template < class T > constexpr basic_string& replace( size_type pos, size_type count, |
(C++20 起) | |
| (8) | ||
| template < class T > basic_string& replace( const_iterator first, const_iterator last, |
(C++17 起) (C++20 前) |
|
| template < class T > constexpr basic_string& replace( const_iterator first, const_iterator last, |
(C++20 起) | |
| (9) | ||
| template < class T > basic_string& replace( size_type pos, size_type count, const T& t, |
(C++17 起) (C++20 前) |
|
| template < class T > constexpr basic_string& replace( size_type pos, size_type count, const T& t, |
(C++20 起) | |
以新字符串替换 [pos, pos + count) 或 [first, last) 所指示的 string 部分。
新字符串可以是下列之一:
str ;str 的子串 [pos2, pos2 + count2) ,除了若 count2==npos 或若它可能越过 str.size() ,则使用 [pos2, str.size()) 。[first2, last2) 中的字符。
|
若 |
(C++11 前) |
|
此重载仅若 |
(C++11 起) |
[cstr, cstr + count2) 中的字符;[cstr, cstr + Traits::length(cstr)) 中的字符;ch 的 count2 个副本;ilist 中的字符;t 转换而来的 string_view sv 的字符。这些重载仅若std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> 为 true 且 std::is_convertible_v<const T&, const CharT*> 为 false 才参与重载决议t 转换而得的字符串视图 sv 的子视图 [pos2, pos2 + count2) ,除了若 count2==npos 或它将越过 sv.size() ,则使用 [pos2, sv.size()) 。此重载仅若 std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> 为 true 且 std::is_convertible_v<const T&, const CharT*> 为 false 才参与重载决议。参数
| pos | - | 将被替换的子串起始位置 |
| count | - | 将被替换的子串长度 |
| first, last | - | 将被替换的字符范围 |
| str | - | 用于替换的 string |
| pos2 | - | 用于替换的子串起始位置 |
| count2 | - | 用于替换的字符数 |
| cstr | - | 指向用于替换的字符串的指针 |
| ch | - | 用于替换的字符值 |
| first2, last2 | - | 用于替换的字符范围 |
| ilist | - | 拥有用于替换的字符的 initializer_list |
| t | - | 拥有用于替换的字符的对象(可转换为 std::basic_string_view ) |
返回值
*this
异常
若 pos > length() 或 pos2 > str.length() 则为 std::out_of_range
若产生的 string 将超出最大可能 string 长度( max_size() - 1 )则为 std::length_error 。
任何情况下,若因任何原因抛出异常,则此函数无效果(强异常保证)。 (C++11 起)
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| DR | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 2946 | C++17 | string_view 重载在某些情况下导致歧义
|
通过使之为模板来避免 |
示例
#include <iostream> #include <string> int main() { std::string str("The quick brown fox jumps over the lazy dog."); str.replace(10, 5, "red"); // (5) str.replace(str.begin(), str.begin() + 3, 1, 'A'); // (6) std::cout << str << '\n'; }
输出:
A quick red fox jumps over the lazy dog.