std::basic_string::replace
性病:基本[医]字符串::替换
basic_string& replace( size_type pos, size_type count, const basic_string& str | (1) | |
---|---|---|
basic_string& replace( const_iterator first, const_iterator last, const basic_string& str | (1) | |
| (2) | |
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 | (since C++14) | |
template< class InputIt > basic_string& replace( const_iterator first, const_iterator last, InputIt first2, InputIt last2 | (3) | |
basic_string& replace( size_type pos, size_type count, const CharT* cstr, size_type count2 | (4) | |
basic_string& replace( const_iterator first, const_iterator last, const CharT* cstr, size_type count2 | (4) | |
basic_string& replace( size_type pos, size_type count, const CharT* cstr | (5) | |
basic_string& replace( const_iterator first, const_iterator last, const CharT* cstr | (5) | |
basic_string& replace( size_type pos, size_type count, size_type count2, CharT ch | (6) | |
basic_string& replace( const_iterator first, const_iterator last, size_type count2, CharT ch | (6) | |
basic_string& replace( const_iterator first, const_iterator last, std::initializer_list<CharT> ilist | (7) | (since C++11) |
basic_string& replace( size_type pos, size_type count, std::basic_string_view<CharT, Traits> sv | (8) | (since C++17) |
basic_string& replace( const_iterator first, const_iterator last, std::basic_string_view<CharT, Traits> sv | (9) | (since C++17) |
template < class T > basic_string& replace( size_type pos, size_type count, const T& t, size_type pos2, size_type count2 = npos | (10) | (since C++17) |
替换由[pos, pos + count)
或[first, last)
用新的绳子。
新字符串可以是:
1%29字符串str
;
2%29子串[pos2, pos2 + count2)
成str
,除非count2==npos
或者是否会延伸到过去str.size()
,,,[pos2, str.size())
被使用了。
3%29个字符在范围内[first2, last2)
。此重载与重载%286%29的效果相同,如果InputIt
是一个整体类型。
4%29首count2
所指向的字符串的字符。cstr
;
所指向的5%29空终止字符串。cstr
;
6%29count2
字符副本ch
;
初始化程序列表中的7%29个字符ilist
;
字符串视图中的8%29个字符sv
28%,相当于replace(pos, count, sv.data(), sv.size())
%29
字符串视图中的9%29个字符sv
28%,相当于replace(first - begin(), last - first, sv)
%29
10%29次浏览[pos2, pos2 + count2)字符串视图的sv,转换自t好像std::basic_string_view<CharT, Traits> sv = t;,除非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 | - | start of the substring that is going to be replaced |
---|---|---|
count | - | length of the substring that is going to be replaced |
first, last | - | range of characters that is going to be replaced |
str | - | string to use for replacement |
pos2 | - | start of the substring to replace with |
count2 | - | number of characters to replace with |
cstr | - | pointer to the character string to use for replacement |
ch | - | character value to use for replacement |
first2, last2 | - | range of characters to use for replacement |
ilist | - | initializer list with the characters to use for replacement |
sv | - | std::basic_string_view with the characters to use for replacement |
t | - | object (convertible to std::basic_string_view) with the characters to use for replacement |
返回值
*this
...
例外
std::out_of_range如果pos > length()或pos2 > str.length()...
std::length_error
如果结果字符串将超过最大可能字符串长度%28std::string::npos
- 1
29%。
在任何情况下,如果出于任何原因引发异常,则此函数不具有%28强异常保证%29的效果。%28自C++11%29。
例
二次
#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.
二次
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。