string::replace

Replace a substring with another string.

Synopsis

string&
replace(
    std::size_t pos,
    std::size_t count,
    string_view sv); (1)

string&
replace(
    string::const_iterator first,
    string::const_iterator last,
    string_view sv); (2)

template<
    class InputIt>
string&
replace(
    string::const_iterator first,
    string::const_iterator last,
    InputIt first2,
    InputIt last2); (3)

string&
replace(
    std::size_t pos,
    std::size_t count,
    std::size_t count2,
    char ch); (4)

string&
replace(
    string::const_iterator first,
    string::const_iterator last,
    std::size_t count2,
    char ch); (5)

Description

  • (1) replaces std::min(count, size() - pos) characters starting at index pos with those of sv.

  • (2) replaces the characters in the range [first, last) with those of sv.

  • (3) replaces the characters in the range [first, last) with those of [first2, last2).

  • (4) replaces std::min(count, size() - pos) characters starting at index pos with count2 copies of ch.

  • (5) replaces the characters in the range [first, last) with count2 copies of ch.

All references, pointers, or iterators referring to contained elements are invalidated. Any past-the-end iterators are also invalidated.

Preconditions

[first, last) is a valid range. [first2, last2) is a valid range.

Constraints

InputIt satisfies LegacyInputIterator.

Exception Safety

Strong guarantee.

Template Parameters

Type Description

InputIt

The type of the iterators.

Return Value

*this

Parameters

Name Description

pos

The index to replace at.

count

The number of characters to replace.

sv

The string_view to replace with.

first

An iterator referring to the first character to replace.

last

An iterator one past the end of the last character to replace.

first2

An iterator referring to the first character to replace with.

last2

An iterator one past the end of the last character to replace with.

count2

The number of characters to replace with.

ch

The character to replace with.

Exceptions

Type Thrown On

boost::system::system_error

The resulting string’s size would have exceeded max_size().