string::assign

Assign characters to a string.

Synopsis

string&
assign(
    std::size_t count,
    char ch); (1)

string&
assign(
    char const* s,
    std::size_t count); (2)

string&
assign(
    char const* s); (3)

template<
    class InputIt>
string&
assign(
    InputIt first,
    InputIt last); (4)

string&
assign(
    string_view s); (5)

string&
assign(
    string const& other); (6)

string&
assign(
    string&& other); (7)

Description

  • (1) replaces the contents with count copies of character ch.

  • (2) replaces the contents with copies of the characters in the range [s, s + count). This range can contain null characters.

  • (3) replaces the contents with those of the null terminated string s. The length of the string is determined by the first null character.

  • (4) replaces the contents with copies of characters in the range [first, last).

  • (5) Replaces the contents with those of string view s. This view can contain null characters.

  • (6) replaces the contents with a copy of the characters of other.

  • (7) if *storage() == *other.storage() takes ownership of the element storage of other; otherwise equivalent to (6).

Self-assignment using (7) does nothing.

After (7) other is left in valid but unspecified state.

Constraints

InputIt satisfies LegacyInputIterator.

Complexity

  • (1), (2) linear in count.

  • (3) linear in std::strlen(s).

  • (4) linear in std::distance(first, last).

  • (5) linear in s.size().

  • (6) linear in other.size().

  • (7) constant if *storage() == *other.storage(), otherwise linear in other.size().

Exception Safety

(7) provides strong guarantee if *storage() != *other.storage() and no-throw guarantee otherwise. Other overloads provide strong guarantee. Calls to memory_resource::allocate may throw.

Template Parameters

Type Description

InputIt

The type of the iterators.

Return Value

*this.

Parameters

Name Description

count

The number of the characters to use.

ch

The character to fill the string with.

s

A pointer to a character string used to copy from.

first

An input iterator pointing to the first character to insert, or pointing to the end of the range.

last

An input iterator pointing to the end of the range.

other

Another string.

Exceptions

Type Thrown On

boost::system::system_error

The size of the string after the operation would exceed max_size().