[string.insert]

# 27 Strings library [[strings]](./#strings)

## 27.4 String classes [[string.classes]](string.classes#string.insert)

### 27.4.3 Class template basic_string [[basic.string]](basic.string#string.insert)

#### 27.4.3.7 Modifiers [[string.modifiers]](string.modifiers#string.insert)

#### 27.4.3.7.4 basic_string​::​insert [string.insert]

[ð](#lib:insert,basic_string)

`constexpr basic_string& insert(size_type pos, const basic_string& str);
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3614)

*Effects*: Equivalent to: return insert(pos, str.data(), str.size());

[ð](#lib:insert,basic_string_)

`constexpr basic_string& insert(size_type pos1, const basic_string& str,
                               size_type pos2, size_type n = npos);
`

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3626)

*Effects*: Equivalent to:return insert(pos1, basic_string_view<charT, traits>(str), pos2, n);

[ð](#lib:insert,basic_string__)

`template<class T>
  constexpr basic_string& insert(size_type pos, const T& t);
`

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3641)

*Constraints*: 

- [(3.1)](#3.1)

is_convertible_v<const T&, basic_string_view<charT, traits>> istrue and

- [(3.2)](#3.2)

is_convertible_v<const T&, const charT*> isfalse[.](#3.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3652)

*Effects*: Equivalent to:basic_string_view<charT, traits> sv = t;return insert(pos, sv.data(), sv.size());

[ð](#lib:insert,basic_string___)

`template<class T>
  constexpr basic_string& insert(size_type pos1, const T& t,
                                 size_type pos2, size_type n = npos);
`

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3669)

*Constraints*: 

- [(5.1)](#5.1)

is_convertible_v<const T&, basic_string_view<charT, traits>> istrue and

- [(5.2)](#5.2)

is_convertible_v<const T&, const charT*> isfalse[.](#5.sentence-1)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3680)

*Effects*: Equivalent to:basic_string_view<charT, traits> sv = t;return insert(pos1, sv.substr(pos2, n));

[ð](#lib:insert,basic_string____)

`constexpr basic_string& insert(size_type pos, const charT* s, size_type n);
`

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3695)

*Preconditions*: [s, s + n) is a valid range[.](#7.sentence-1)

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3699)

*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[.](#8.sentence-1)

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3705)

*Returns*: *this[.](#9.sentence-1)

[10](#10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3709)

*Throws*: 

- [(10.1)](#10.1)

out_of_range if pos > size(),

- [(10.2)](#10.2)

length_error if n > max_size() - size(), or

- [(10.3)](#10.3)

any exceptions thrown by allocator_traits<Allocator>​::​allocate[.](#10.sentence-1)

[ð](#lib:insert,basic_string_____)

`constexpr basic_string& insert(size_type pos, const charT* s);
`

[11](#11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3724)

*Effects*: Equivalent to: return insert(pos, s, traits​::​length(s));

[ð](#lib:insert,basic_string______)

`constexpr basic_string& insert(size_type pos, size_type n, charT c);
`

[12](#12)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3735)

*Effects*: Inserts n copies of c before the character at position pos if pos < size(),
or otherwise at the end of the string[.](#12.sentence-1)

[13](#13)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3741)

*Returns*: *this[.](#13.sentence-1)

[14](#14)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3745)

*Throws*: 

- [(14.1)](#14.1)

out_of_range if pos > size(),

- [(14.2)](#14.2)

length_error if n > max_size() - size(), or

- [(14.3)](#14.3)

any exceptions thrown by allocator_traits<Allocator>​::​allocate[.](#14.sentence-1)

[ð](#lib:insert,basic_string_______)

`constexpr iterator insert(const_iterator p, charT c);
`

[15](#15)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3760)

*Preconditions*: p is a valid iterator on*this[.](#15.sentence-1)

[16](#16)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3765)

*Effects*: Inserts a copy of c at the position p[.](#16.sentence-1)

[17](#17)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3769)

*Returns*: An iterator which refers to the inserted character[.](#17.sentence-1)

[ð](#lib:insert,basic_string________)

`constexpr iterator insert(const_iterator p, size_type n, charT c);
`

[18](#18)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3780)

*Preconditions*: p is a valid iterator on*this[.](#18.sentence-1)

[19](#19)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3785)

*Effects*: Inserts n copies of c at the position p[.](#19.sentence-1)

[20](#20)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3789)

*Returns*: An iterator which refers to the first inserted character, orp if n == 0[.](#20.sentence-1)

[ð](#lib:insert,basic_string_________)

`template<class InputIterator>
  constexpr iterator insert(const_iterator p, InputIterator first, InputIterator last);
`

[21](#21)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3802)

*Constraints*: InputIterator is a type that qualifies as an input
iterator ([[container.reqmts]](container.reqmts "23.2.2.2 Container requirements"))[.](#21.sentence-1)

[22](#22)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3807)

*Preconditions*: p is a valid iterator on*this[.](#22.sentence-1)

[23](#23)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3812)

*Effects*: Equivalent toinsert(p - begin(), basic_string(first, last, get_allocator()))[.](#23.sentence-1)

[24](#24)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3817)

*Returns*: An iterator which refers to the first inserted character, orp if first == last[.](#24.sentence-1)

[ð](#lib:insert_range,basic_string)

`template<[container-compatible-range](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]")<charT> R>
  constexpr iterator insert_range(const_iterator p, R&& rg);
`

[25](#25)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3830)

*Preconditions*: p is a valid iterator on *this[.](#25.sentence-1)

[26](#26)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3834)

*Effects*: Equivalent toinsert(p - begin(), basic_string(from_range, std​::​forward<R>(rg), get_allocator()))[.](#26.sentence-1)

[27](#27)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3839)

*Returns*: An iterator which refers to the first inserted character, orp if rg is empty[.](#27.sentence-1)

[ð](#lib:insert,basic_string__________)

`constexpr iterator insert(const_iterator p, initializer_list<charT> il);
`

[28](#28)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3851)

*Effects*: Equivalent to: return insert(p, il.begin(), il.end());
