std::basic_string<CharT,Traits,Allocator>::replace
来自cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
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) 中的字符替换那些字符。6,7) 以范围
[cstr, cstr + Traits::length(cstr)) 中的字符替换那些字符。8,9) 以
count2 个 ch 的副本替换那些字符。10) 如同用
replace(first, last, basic_string(first2, last2, get_allocator())) 以范围[first2, last2) 中的字符替换那些字符。
11) 以
ilist 中的字符替换那些字符。12,13) 如同用
std::basic_string_view<CharT, Traits> sv = t; 将 t 隐式转换到字符串视图 sv ,然后以 sv 中的字符替换那些字符。 这些重载只有在
std::is_convertible_v<const StringViewLike&,std::basic_string_view<CharT, Traits>> 是 true 且 std::is_convertible_v<const StringViewLike&, const CharT*> 是 false 时才会参与重载决议。
14) 如同用
std::basic_string_view<CharT, Traits> sv = t; 将 t 隐式转换到字符串视图 sv ,然后以子视图 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。
如果因为任何原因抛出了异常,那么此函数无效果(强异常安全保证)。
示例
运行此代码
#include <string>
#include <iostream>
int main()
{
const std::string str = "C++ primer 4ths";
const std::string replace = "is a books";
// 从位置 11 开始删除 4 个字符并添加 "5th"
std::string replace_str1 = str;
replace_str1.replace(11, 4, "5th");
std::cout << replace_str1 << '\n';
// 同上
std::string replace_str2 = str;
replace_str2.replace(replace_str2.end() - 4,replace_str2.end(),"6th");
std::cout << replace_str2 << '\n';
// 从位置 11 开始删除 4 个字符并添加 "is a book"
std::string replace_str3 = str;
replace_str3.replace(11, 4, replace, 0, replace.size() - 1);
std::cout << replace_str3 << '\n';
}
输出:
1) C++ primer 5th
2) C++ primer 6th
3) C++ primer is a book
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 847 | C++98 | 没有异常安全保证 | 添加强异常安全保证 |
| LWG 1323 | C++98 | first 和 last 的类型是 iterator
|
改成 const_iterator
|
| LWG 2946 | C++17 | 重载 (12,13) 在某些情况下会导致歧义 | 通过使之为模板来避免 |
参阅
(C++23) |
以范围中的字符替换字符串的指定部分 (公开成员函数) |
(C++11) |
以格式化的替换文本来替换正则表达式匹配的出现位置 (函数模板) |
| 替换所有满足特定条件的值为另一个值 (函数模板) |