23 Strings library [strings]

23.4 String classes [string.classes]

23.4.3 Class template basic_string [basic.string]

23.4.3.7 Modifiers [string.modifiers]

23.4.3.7.4 basic_string​::​insert [string.insert]

constexpr basic_string& insert(size_type pos, const basic_string& str);
Effects: Equivalent to: return insert(pos, str.data(), str.size());
constexpr basic_string& insert(size_type pos1, const basic_string& str, size_type pos2, size_type n = npos);
Effects: Equivalent to: return insert(pos1, basic_string_view<charT, traits>(str), pos2, n);
template<class T> constexpr basic_string& insert(size_type pos, const T& t);
Constraints:
  • is_convertible_v<const T&, basic_string_view<charT, traits>> is true and
  • is_convertible_v<const T&, const charT*> is false.
Effects: Equivalent to: basic_string_view<charT, traits> sv = t; return insert(pos, sv.data(), sv.size());
template<class T> constexpr basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n = npos);
Constraints:
  • is_convertible_v<const T&, basic_string_view<charT, traits>> is true and
  • is_convertible_v<const T&, const charT*> is false.
Effects: Equivalent to: basic_string_view<charT, traits> sv = t; return insert(pos1, sv.substr(pos2, n));
constexpr basic_string& insert(size_type pos, const charT* s, size_type n);
Preconditions: [s, s + n) is a valid range.
Effects: Inserts a copy of the range [s, s + n) immediately before the character at position pos if pos < size(), or otherwise at the end of the string.
Returns: *this.
Throws:
  • out_of_range if pos > size(),
  • length_error if n > max_size() - size(), or
  • any exceptions thrown by allocator_traits<Allocator>​::​allocate.
constexpr basic_string& insert(size_type pos, const charT* s);
Effects: Equivalent to: return insert(pos, s, traits​::​length(s));
constexpr basic_string& insert(size_type pos, size_type n, charT c);
Effects: Inserts n copies of c before the character at position pos if pos < size(), or otherwise at the end of the string.
Returns: *this
Throws:
  • out_of_range if pos > size(),
  • length_error if n > max_size() - size(), or
  • any exceptions thrown by allocator_traits<Allocator>​::​allocate.
constexpr iterator insert(const_iterator p, charT c);
Preconditions: p is a valid iterator on *this.
Effects: Inserts a copy of c at the position p.
Returns: An iterator which refers to the inserted character.
constexpr iterator insert(const_iterator p, size_type n, charT c);
Preconditions: p is a valid iterator on *this.
Effects: Inserts n copies of c at the position p.
Returns: An iterator which refers to the first inserted character, or p if n == 0.
template<class InputIterator> constexpr iterator insert(const_iterator p, InputIterator first, InputIterator last);
Constraints: InputIterator is a type that qualifies as an input iterator ([container.reqmts]).
Preconditions: p is a valid iterator on *this.
Effects: Equivalent to insert(p - begin(), basic_string(first, last, get_allocator())).
Returns: An iterator which refers to the first inserted character, or p if first == last.
template<container-compatible-range<charT> R> constexpr iterator insert_range(const_iterator p, R&& rg);
Preconditions: p is a valid iterator on *this.
Effects: Equivalent to insert(p - begin(), basic_string(from_range, std​::​forward<R>(rg), get_allocator())).
Returns: An iterator which refers to the first inserted character, or p if rg is empty.
constexpr iterator insert(const_iterator p, initializer_list<charT> il);
Effects: Equivalent to: return insert(p, il.begin(), il.end());