24 Strings library [strings]

24.3 String classes [string.classes]

24.3.2 Class template basic_­string [basic.string]

24.3.2.6 basic_­string modifiers [string.modifiers]

24.3.2.6.1 basic_­string​::​operator+= [string.op+=]

basic_string& operator+=(const basic_string& str);
Effects: Calls append(str).
Returns: *this.
basic_string& operator+=(basic_string_view<charT, traits> sv);
Effects: Calls append(sv).
Returns: *this.
basic_string& operator+=(const charT* s);
Effects: Calls append(s).
Returns: *this.
basic_string& operator+=(charT c);
Effects: Calls push_­back(c);
Returns: *this.
basic_string& operator+=(initializer_list<charT> il);
Effects: Calls append(il).
Returns: *this.

24.3.2.6.2 basic_­string​::​append [string.append]

basic_string& append(const basic_string& str);
Effects: Calls append(str.data(), str.size()).
Returns: *this.
basic_string& append(const basic_string& str, size_type pos, size_type n = npos);
Throws: out_­of_­range if pos > str.size().
Effects: Determines the effective length rlen of the string to append as the smaller of n and str.size() - pos and calls append(str.data() + pos, rlen).
Returns: *this.
basic_string& append(basic_string_view<charT, traits> sv);
Effects: Equivalent to: return append(sv.data(), sv.size());
template<class T> basic_string& append(const T& t, size_type pos, size_type n = npos);
Throws: out_­of_­range if pos > sv.size().
Effects: Creates a variable, sv, as if by basic_­string_­view<charT, traits> sv = t.
Determines the effective length rlen of the string to append as the smaller of n and sv.size() - pos and calls append(sv.data() + pos, rlen).
Remarks: This function shall not participate in overload resolution unless is_­convertible_­v<const T&, basic_­string_­view<charT, traits>> is true and is_­convertible_­v<const T&, const charT*> is false.
Returns: *this.
basic_string& append(const charT* s, size_type n);
Requires: s points to an array of at least n elements of charT.
Throws: length_­error if size() + n > max_­size().
Effects: The function replaces the string controlled by *this with a string of length size() + n whose first size() elements are a copy of the original string controlled by *this and whose remaining elements are a copy of the initial n elements of s.
Returns: *this.
basic_string& append(const charT* s);
Requires: s points to an array of at least traits​::​length(s) + 1 elements of charT.
Effects: Calls append(s, traits​::​length(s)).
Returns: *this.
basic_string& append(size_type n, charT c);
Effects: Equivalent to append(basic_­string(n, c)).
Returns: *this.
template<class InputIterator> basic_string& append(InputIterator first, InputIterator last);
Requires: [first, last) is a valid range.
Effects: Equivalent to append(basic_­string(first, last, get_­allocator())).
Returns: *this.
basic_string& append(initializer_list<charT> il);
Effects: Calls append(il.begin(), il.size()).
Returns: *this.
void push_back(charT c);
Effects: Equivalent to append(static_­cast<size_­type>(1), c).

24.3.2.6.3 basic_­string​::​assign [string.assign]

basic_string& assign(const basic_string& str);
Effects: Equivalent to *this = str.
Returns: *this.
basic_string& assign(basic_string&& str) noexcept(allocator_traits<Allocator>::propagate_on_container_move_assignment::value || allocator_traits<Allocator>::is_always_equal::value);
Effects: Equivalent to *this = std​::​move(str).
Returns: *this.
basic_string& assign(const basic_string& str, size_type pos, size_type n = npos);
Throws: out_­of_­range if pos > str.size().
Effects: Determines the effective length rlen of the string to assign as the smaller of n and str.size() - pos and calls assign(str.data() + pos, rlen).
Returns: *this.
basic_string& assign(basic_string_view<charT, traits> sv);
Effects: Equivalent to: return assign(sv.data(), sv.size());
template<class T> basic_string& assign(const T& t, size_type pos, size_type n = npos);
Throws: out_­of_­range if pos > sv.size().
Effects: Creates a variable, sv, as if by basic_­string_­view<charT, traits> sv = t.
Determines the effective length rlen of the string to assign as the smaller of n and sv.size() - pos and calls assign(sv.data() + pos, rlen).
Remarks: This function shall not participate in overload resolution unless is_­convertible_­v<const T&, basic_­string_­view<charT, traits>> is true and is_­convertible_­v<const T&, const charT*> is false.
Returns: *this.
basic_string& assign(const charT* s, size_type n);
Requires: s points to an array of at least n elements of charT.
Throws: length_­error if n > max_­size().
Effects: Replaces the string controlled by *this with a string of length n whose elements are a copy of those pointed to by s.
Returns: *this.
basic_string& assign(const charT* s);
Requires: s points to an array of at least traits​::​length(s) + 1 elements of charT.
Effects: Calls assign(s, traits​::​length(s)).
Returns: *this.
basic_string& assign(initializer_list<charT> il);
Effects: Calls assign(il.begin(), il.size()).
Returns: *this.
basic_string& assign(size_type n, charT c);
Effects: Equivalent to assign(basic_­string(n, c)).
Returns: *this.
template<class InputIterator> basic_string& assign(InputIterator first, InputIterator last);
Effects: Equivalent to assign(basic_­string(first, last, get_­allocator())).
Returns: *this.

24.3.2.6.4 basic_­string​::​insert [string.insert]

basic_string& insert(size_type pos, const basic_string& str);
Effects: Equivalent to: return insert(pos, str.data(), str.size());
basic_string& insert(size_type pos1, const basic_string& str, size_type pos2, size_type n = npos);
Throws: out_­of_­range if pos1 > size() or pos2 > str.size().
Effects: Determines the effective length rlen of the string to insert as the smaller of n and str.size() - pos2 and calls insert(pos1, str.data() + pos2, rlen).
Returns: *this.
basic_string& insert(size_type pos, basic_string_view<charT, traits> sv);
Effects: Equivalent to: return insert(pos, sv.data(), sv.size());
template<class T> basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n = npos);
Throws: out_­of_­range if pos1 > size() or pos2 > sv.size().
Effects: Creates a variable, sv, as if by basic_­string_­view<charT, traits> sv = t.
Determines the effective length rlen of the string to assign as the smaller of n and sv.size() - pos2 and calls insert(pos1, sv.data() + pos2, rlen).
Remarks: This function shall not participate in overload resolution unless is_­convertible_­v<const T&, basic_­string_­view<charT, traits>> is true and is_­convertible_­v<const T&, const charT*> is false.
Returns: *this.
basic_string& insert(size_type pos, const charT* s, size_type n);
Requires: s points to an array of at least n elements of charT.
Throws: out_­of_­range if pos > size() or length_­error if size() + n > max_­size().
Effects: Replaces the string controlled by *this with a string of length size() + n whose first pos elements are a copy of the initial elements of the original string controlled by *this and whose next n elements are a copy of the elements in s and whose remaining elements are a copy of the remaining elements of the original string controlled by *this.
Returns: *this.
basic_string& insert(size_type pos, const charT* s);
Requires: s points to an array of at least traits​::​length(s) + 1 elements of charT.
Effects: Equivalent to: return insert(pos, s, traits​::​length(s));
basic_string& insert(size_type pos, size_type n, charT c);
Effects: Equivalent to insert(pos, basic_­string(n, c)).
Returns: *this.
iterator insert(const_iterator p, charT c);
Requires: p is a valid iterator on *this.
Effects: Inserts a copy of c before the character referred to by p.
Returns: An iterator which refers to the copy of the inserted character.
iterator insert(const_iterator p, size_type n, charT c);
Requires: p is a valid iterator on *this.
Effects: Inserts n copies of c before the character referred to by p.
Returns: An iterator which refers to the copy of the first inserted character, or p if n == 0.
template<class InputIterator> iterator insert(const_iterator p, InputIterator first, InputIterator last);
Requires: p is a valid iterator on *this.
[first, last) is a valid range.
Effects: Equivalent to insert(p - begin(), basic_­string(first, last, get_­allocator())).
Returns: An iterator which refers to the copy of the first inserted character, or p if first == last.
iterator insert(const_iterator p, initializer_list<charT> il);
Effects: As if by insert(p, il.begin(), il.end()).
Returns: An iterator which refers to the copy of the first inserted character, or p if i1 is empty.

24.3.2.6.5 basic_­string​::​erase [string.erase]

basic_string& erase(size_type pos = 0, size_type n = npos);
Throws: out_­of_­range if pos > size().
Effects: Determines the effective length xlen of the string to be removed as the smaller of n and size() - pos.
The function then replaces the string controlled by *this with a string of length size() - xlen whose first pos elements are a copy of the initial elements of the original string controlled by *this, and whose remaining elements are a copy of the elements of the original string controlled by *this beginning at position pos + xlen.
Returns: *this.
iterator erase(const_iterator p);
Throws: Nothing.
Effects: Removes the character referred to by p.
Returns: An iterator which points to the element immediately following p prior to the element being erased.
If no such element exists, end() is returned.
iterator erase(const_iterator first, const_iterator last);
Requires: first and last are valid iterators on *this, defining a range [first, last).
Throws: Nothing.
Effects: Removes the characters in the range [first, last).
Returns: An iterator which points to the element pointed to by last prior to the other elements being erased.
If no such element exists, end() is returned.
void pop_back();
Requires: !empty().
Throws: Nothing.
Effects: Equivalent to erase(size() - 1, 1).

24.3.2.6.6 basic_­string​::​replace [string.replace]

basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Effects: Equivalent to: return replace(pos1, n1, str.data(), str.size());
basic_string& replace(size_type pos1, size_type n1, const basic_string& str, size_type pos2, size_type n2 = npos);
Throws: out_­of_­range if pos1 > size() or pos2 > str.size().
Effects: Determines the effective length rlen of the string to be inserted as the smaller of n2 and str.size() - pos2 and calls replace(pos1, n1, str.data() + pos2, rlen).
Returns: *this.
basic_string& replace(size_type pos1, size_type n1, basic_string_view<charT, traits> sv);
Effects: Equivalent to: return replace(pos1, n1, sv.data(), sv.size());
template<class T> basic_string& replace(size_type pos1, size_type n1, const T& t, size_type pos2, size_type n2 = npos);
Throws: out_­of_­range if pos1 > size() or pos2 > sv.size().
Effects: Creates a variable, sv, as if by basic_­string_­view<charT, traits> sv = t.
Determines the effective length rlen of the string to be inserted as the smaller of n2 and sv.size() - pos2 and calls replace(pos1, n1, sv.data() + pos2, rlen).
Remarks: This function shall not participate in overload resolution unless is_­convertible_­v<const T&, basic_­string_­view<charT, traits>> is true and is_­convertible_­v<const T&, const charT*> is false.
Returns: *this.
basic_string& replace(size_type pos1, size_type n1, const charT* s, size_type n2);
Requires: s points to an array of at least n2 elements of charT.
Throws: out_­of_­range if pos1 > size() or length_­error if the length of the resulting string would exceed max_­size() (see below).
Effects: Determines the effective length xlen of the string to be removed as the smaller of n1 and size() - pos1.
If size() - xlen >= max_­size() - n2 throws length_­error.
Otherwise, the function replaces the string controlled by *this with a string of length size() - xlen + n2 whose first pos1 elements are a copy of the initial elements of the original string controlled by *this, whose next n2 elements are a copy of the initial n2 elements of s, and whose remaining elements are a copy of the elements of the original string controlled by *this beginning at position pos + xlen.
Returns: *this.
basic_string& replace(size_type pos, size_type n, const charT* s);
Requires: s points to an array of at least traits​::​length(s) + 1 elements of charT.
Effects: Equivalent to: return replace(pos, n, s, traits​::​length(s));
basic_string& replace(size_type pos1, size_type n1, size_type n2, charT c);
Effects: Equivalent to replace(pos1, n1, basic_­string(n2, c)).
Returns: *this.
basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Requires: [begin(), i1) and [i1, i2) are valid ranges.
Effects: Calls replace(i1 - begin(), i2 - i1, str).
Returns: *this.
basic_string& replace(const_iterator i1, const_iterator i2, basic_string_view<charT, traits> sv);
Requires: [begin(), i1) and [i1, i2) are valid ranges.
Effects: Calls replace(i1 - begin(), i2 - i1, sv).
Returns: *this.
basic_string& replace(const_iterator i1, const_iterator i2, const charT* s, size_type n);
Requires: [begin(), i1) and [i1, i2) are valid ranges and s points to an array of at least n elements of charT.
Effects: Calls replace(i1 - begin(), i2 - i1, s, n).
Returns: *this.
basic_string& replace(const_iterator i1, const_iterator i2, const charT* s);
Requires: [begin(), i1) and [i1, i2) are valid ranges and s points to an array of at least traits​::​​length(s) + 1 elements of charT.
Effects: Calls replace(i1 - begin(), i2 - i1, s, traits​::​length(s)).
Returns: *this.
basic_string& replace(const_iterator i1, const_iterator i2, size_type n, charT c);
Requires: [begin(), i1) and [i1, i2) are valid ranges.
Effects: Calls replace(i1 - begin(), i2 - i1, basic_­string(n, c)).
Returns: *this.
template<class InputIterator> basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
Requires: [begin(), i1), [i1, i2) and [j1, j2) are valid ranges.
Effects: Calls replace(i1 - begin(), i2 - i1, basic_­string(j1, j2, get_­allocator())).
Returns: *this.
basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<charT> il);
Requires: [begin(), i1) and [i1, i2) are valid ranges.
Effects: Calls replace(i1 - begin(), i2 - i1, il.begin(), il.size()).
Returns: *this.

24.3.2.6.7 basic_­string​::​copy [string.copy]

size_type copy(charT* s, size_type n, size_type pos = 0) const;
Let rlen be the smaller of n and size() - pos.
Throws: out_­of_­range if pos > size().
Requires: [s, s + rlen) is a valid range.
Effects: Equivalent to: traits​::​copy(s, data() + pos, rlen).
[Note
:
This does not terminate s with a null object.
end note
]
Returns: rlen.

24.3.2.6.8 basic_­string​::​swap [string.swap]

void swap(basic_string& s) noexcept(allocator_traits<Allocator>::propagate_on_container_swap::value || allocator_traits<Allocator>::is_always_equal::value);
Postconditions: *this contains the same sequence of characters that was in s, s contains the same sequence of characters that was in *this.
Throws: Nothing.
Complexity: Constant time.