std::basic_string::insert(3)
std::basic_string::insert(3) πΈ
okay bestie, this is the std::basic_string::insert(3) page from the C++ Standard Libary (their spelling, not mine ππ). It's giving βstring surgery, but make it C++,β and honestly? iconic. You're literally inserting characters into an existing string at an index or before an iterator. No cap.
π NAME π
std::basic_string::insert β std::basic_string::insert
β¨ Synopsis β¨
basic_string& insert(size_type index, size_type count, CharT ch); // (1) constexpr since C++20
basic_string& insert(size_type index, const CharT* s); // (2) constexpr since C++20
basic_string& insert(size_type index, const CharT* s, size_type count); // (3) constexpr since C++20
basic_string& insert(size_type index, const basic_string& str); // (4) constexpr since C++20
basic_string& insert(size_type index, const basic_string& str,
size_type s_index, size_type count); // (5) until C++14
basic_string& insert(size_type index, const basic_string& str,
size_type s_index, size_type count = npos); // (5) since C++14; constexpr since C++20
iterator insert(iterator pos, CharT ch); // (6) until C++11
iterator insert(const_iterator pos, CharT ch); // (6) since C++11; constexpr since C++20
void insert(iterator pos, size_type count, CharT ch); // (7) until C++11
iterator insert(const_iterator pos, size_type count, CharT ch); // (7) since C++11; constexpr since C++20
template<class InputIt>
void insert(iterator pos, InputIt first, InputIt last); // (8) until C++11
template<class InputIt>
iterator insert(const_iterator pos, InputIt first, InputIt last); // (8) since C++11; constexpr since C++20
iterator insert(const_iterator pos, std::initializer_list<CharT> ilist); // (9) since C++11; constexpr since C++20
template<class StringViewLike>
basic_string& insert(size_type index, const StringViewLike& t); // (10) since C++17; constexpr since C++20
template<class StringViewLike>
basic_string& insert(size_type index, const StringViewLike& t,
size_type t_index, size_type count = npos); // (11) since C++17; constexpr since C++20
Inserts characters into the string. Yeah, that's the whole vibe: characters go in, string gets longer, we celebrate π
- (1) Inserts
countcopies of characterchat the positionindex. - (2) Inserts null-terminated character string pointed to by
sat the positionindex. The length of the string is determined by the first null character usingTraits::length(s). - (3) Inserts the characters in the range
[s, s + count)at the positionindex. The range can contain null characters. - (4) Inserts string
strat the positionindex. - (5) Inserts a string, obtained by
str.substr(s_index, count)at the positionindex. - (6) Inserts character
chbefore the character pointed bypos. - (7) Inserts
countcopies of characterchbefore the element (if any) pointed bypos. - (8) Inserts characters from the range
[first, last)before the element (if any) pointed bypos, as if byinsert(pos - begin(), basic_string(first, last, get_allocator())).- This overload does not participate in overload resolution if
InputIt(since C++11) does not satisfyLegacyInputIterator.
- This overload does not participate in overload resolution if
- (9) Inserts elements from initializer list
ilistbefore the element (if any) pointed bypos. - (10) Implicitly converts
tto a string viewsvas if bystd::basic_string_view<CharT, Traits> sv = t;, then inserts the elements fromsvbefore the element (if any) pointed byindex, as if byinsert(index, sv.data(), sv.size()).- This overload participates in overload resolution only if
std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>is true andstd::is_convertible_v<const StringViewLike&, const CharT*>is false.
- This overload participates in overload resolution only if
- (11) Implicitly converts
tto a string viewsvas if bystd::basic_string_view<CharT, Traits> sv = t;, then inserts, before the element (if any) pointed byindex, the characters from the subview[t_index, t_index + count)ofsv.- If the requested subview lasts past the end of
sv, or ifcount == npos, the resulting subview is[t_index, sv.size()). - If
t_index > sv.size(), or ifindex > size(),std::out_of_rangeis thrown. - This overload participates in overload resolution only if
std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>is true andstd::is_convertible_v<const StringViewLike&, const CharT*>is false.
- If the requested subview lasts past the end of
Ifposis not a valid iterator on*this, the behavior is undefined. bestie, respect the iterator or face the consequences π
π¦ Parameters π¦
indexβ position at which the content will be insertedposβ iterator before which the characters will be insertedchβ character to insertcountβ number of characters to insertsβ pointer to the character string to insertstrβ string to insertfirst, lastβ range defining characters to inserts_indexβ position of the first character instrto insertilistβstd::initializer_listto insert the characters fromtβ object (convertible tostd::basic_string_view) to insert the characters fromt_indexβ position of the first character intto insert
π§ Type requirements π§
InputItmust meet the requirements ofLegacyInputIterator.
π Return value π
- 1β5)
*this - 6β9) An iterator which refers to the copy of the first inserted character or
posif no characters were inserted (count == 0orfirst == lastorilist.size() == 0) - 10,11)
*this
π₯ Exceptions π₯
- 1β4,10) Throws
std::out_of_rangeifindex > size(). - 5) Throws
std::out_of_rangeifindex > size()or ifs_index > str.size(). - 11) Throws
std::out_of_rangeifindex > size()or ift_index > sv.size().
In all cases, throws std::length_error if size() + ins_count > max_size() where ins_count is the number of characters that will be inserted.
In all cases, if std::allocator_traits<Allocator>::allocate throws an exception, it is rethrown. (since C++20)
If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee). yes bestie, it said strong exception safety guarantee β we love a function that doesn't leave you in a messy situation π«Ά
π Example π
#include <cassert>
#include <iterator>
#include <string>
using namespace std::string_literals;
int main()
{
std::string s = "xmplr";
// insert(size_type index, size_type count, char ch)
s.insert(0, 1, 'E');
assert("Exmplr" == s);
// insert(size_type index, const char* s)
s.insert(2, "e");
assert("Exemplr" == s);
// insert(size_type index, string const& str)
s.insert(6, "a"s);
assert("Exemplar" == s);
// insert(size_type index, string const& str,
// size_type s_index, size_type count)
s.insert(8, " is an example string."s, 0, 14);
assert("Exemplar is an example" == s);
// insert(const_iterator pos, char ch)
s.insert(s.cbegin() + s.find_first_of('n') + 1, ':');
assert("Exemplar is an: example" == s);
// insert(const_iterator pos, size_type count, char ch)
s.insert(s.cbegin() + s.find_first_of(':') + 1, 2, '=');
assert("Exemplar is an:== example" == s);
// insert(const_iterator pos, InputIt first, InputIt last)
{
std::string seq = " string";
s.insert(s.begin() + s.find_last_of('e') + 1,
std::begin(seq), std::end(seq));
assert("Exemplar is an:== example string" == s);
}
// insert(const_iterator pos, std::initializer_list<char>)
s.insert(s.cbegin() + s.find_first_of('g') + 1, {'.'});
assert("Exemplar is an:== example string." == s);
}
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- LWG 7 β Applied to C++98: overload (8) referred to a non-existing overload; correct behavior: refers to overload (4) correctly.
- LWG 847 β Applied to C++98: there was no exception safety guarantee; correct behavior: added strong exception safety guarantee.
- LWG 2946 β Applied to C++17: overload (10) caused ambiguity in some cases; correct behavior: avoided by making it a template.
π See also π
insert_rangeβ inserts a range of characters (C++23) (public member function)appendβ appends characters to the end (public member function)push_backβ appends a character to the end (public member function)
mwah π this page is served fresh from http://cppreference.com, version/date 2024.06.10 β now go slay those insertions, bestie π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms