std::basic_string<CharT,Traits,Allocator>::replace
Min standard notice:
Replaces the characters in the range [begin() + pos,begin() + std::min(pos + count, size())) or [first,last) with given characters.
# Declarations
basic_string& replace( size_type pos, size_type count,
const basic_string& str );
(constexpr since C++20)
basic_string& replace( const_iterator first, const_iterator last,
const basic_string& str );
(constexpr since C++20)
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) (constexpr since C++20)
basic_string& replace( size_type pos, size_type count,
const CharT* cstr, size_type count2 );
(constexpr since C++20)
basic_string& replace( const_iterator first, const_iterator last,
const CharT* cstr, size_type count2 );
(constexpr since C++20)
basic_string& replace( size_type pos, size_type count,
const CharT* cstr );
(constexpr since C++20)
basic_string& replace( const_iterator first, const_iterator last,
const CharT* cstr );
(constexpr since C++20)
basic_string& replace( size_type pos, size_type count,
size_type count2, CharT ch );
(constexpr since C++20)
basic_string& replace( const_iterator first, const_iterator last,
size_type count2, CharT ch );
(constexpr since C++20)
template< class InputIt >
basic_string& replace( const_iterator first, const_iterator last,
InputIt first2, InputIt last2 );
(constexpr since C++20)
basic_string& replace( const_iterator first, const_iterator last,
std::initializer_list<CharT> ilist );
(since C++11) (constexpr since C++20)
template< class StringViewLike >
basic_string& replace( size_type pos, size_type count,
const StringViewLike& t );
(since C++17) (constexpr since C++20)
template< class StringViewLike >
basic_string& replace( const_iterator first, const_iterator last,
const StringViewLike& t );
(since C++17) (constexpr since C++20)
template< class StringViewLike >
basic_string& replace( size_type pos, size_type count,
const StringViewLike& t,
size_type pos2, size_type count2 = npos );
(since C++17) (constexpr since C++20)
# Parameters
pos: start of the substring that is going to be replacedcount: length of the substring that is going to be replacedfirst, last: range of characters that is going to be replacedstr: string to use for replacementpos2: start of the substring to replace withcount2: number of characters to replace withcstr: pointer to the character string to use for replacementch: character value to use for replacementfirst2, last2: range of characters to use for replacementilist: initializer list with the characters to use for replacementt: object (convertible to std::basic_string_view) with the characters to use for replacement
# Return value
*this.
# Example
This section is incompleteReason: no example
# Defect reports
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 847 | C++98 | there was no exception safety guarantee | added strong exception safety guarantee |
| LWG 1323 | C++98 | the types of first and last were iterator | changed to const_iterator |
| LWG 2946 | C++17 | overloads (12,13) caused ambiguity in some cases | avoided by making them templates |