Files
2025-10-25 03:02:53 +03:00

8.2 KiB
Raw Permalink Blame History

[string.insert]

27 Strings library [strings]

27.4 String classes [string.classes]

27.4.3 Class template basic_string [basic.string]

27.4.3.7 Modifiers [string.modifiers]

27.4.3.7.4 basic_string::insert [string.insert]

🔗

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

1

#

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);

2

#

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);

3

#

Constraints:

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

is_convertible_v<const T&, const charT*> isfalse.

4

#

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);

5

#

Constraints:

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

is_convertible_v<const T&, const charT*> isfalse.

6

#

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);

7

#

Preconditions: [s, s + n) is a valid range.

8

#

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.

9

#

Returns: *this.

10

#

Throws:

out_of_range if pos > size(),

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

any exceptions thrown by allocator_traits::allocate.

🔗

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

11

#

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

🔗

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

12

#

Effects: Inserts n copies of c before the character at position pos if pos < size(), or otherwise at the end of the string.

13

#

Returns: *this.

14

#

Throws:

out_of_range if pos > size(),

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

any exceptions thrown by allocator_traits::allocate.

🔗

constexpr iterator insert(const_iterator p, charT c);

15

#

Preconditions: p is a valid iterator on*this.

16

#

Effects: Inserts a copy of c at the position p.

17

#

Returns: An iterator which refers to the inserted character.

🔗

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

18

#

Preconditions: p is a valid iterator on*this.

19

#

Effects: Inserts n copies of c at the position p.

20

#

Returns: An iterator which refers to the first inserted character, orp if n == 0.

🔗

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

21

#

Constraints: InputIterator is a type that qualifies as an input iterator ([container.reqmts]).

22

#

Preconditions: p is a valid iterator on*this.

23

#

Effects: Equivalent toinsert(p - begin(), basic_string(first, last, get_allocator())).

24

#

Returns: An iterator which refers to the first inserted character, orp if first == last.

🔗

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

25

#

Preconditions: p is a valid iterator on *this.

26

#

Effects: Equivalent toinsert(p - begin(), basic_string(from_range, std::forward(rg), get_allocator())).

27

#

Returns: An iterator which refers to the first inserted character, orp if rg is empty.

🔗

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

28

#

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