Home C++ Strings Functions insert() C++ std::string insert() Function Definition In C++, std::string::insert() places new characters at a chosen position . Text that was there moves to the right.
Overloads accept an index, iterators, strings, C-strings, or repeated characters. Include <string>.
This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.
Syntax Index form is the easiest to start with.
s.insert(2, "XX");
s.insert(s.begin(), '!');
s.insert(0, 3, '*'); Call What it does s.insert(pos, str)Insert str starting at index pos. s.insert(pos, n, ch)Insert ch repeated n times at pos. s.insert(it, ch)Insert one character before iterator it.
If pos > size(), index overloads throw std::out_of_range.
Library declarations string& insert(size_type pos, const string& str);
string& insert(size_type pos, size_type n, char ch);
iterator insert(const_iterator pos, char ch); Parameters Parameter Description posIndex or iterator where insertion begins. str / ch / nText or repeated character to insert.
Return Value Return Value Description string& or iteratorIndex overloads return *this; iterator overloads return an iterator to the first inserted character.
C++ Examples 1. Insert in middle 2. Insert stars 3. Insert by iterator Example 1 — Basic insert() Insert text at index 2.
#include <iostream>
#include <string>
int main() {
std::string s = "CFun";
s.insert(1, "odeTo");
std::cout << s << '\n';
return 0;
} How It Works Index 1 sits before 'F' in "CFun". Inserting "odeTo" shifts the rest right. Result is "CodeToFun". Example 2 — Insert Repeated Chars Insert three stars at the front.
#include <iostream>
#include <string>
int main() {
std::string s = "go";
s.insert(0, 3, '*');
std::cout << s << '\n';
return 0;
} How It Works Position 0 is the front. Three '*' characters are inserted. Original text follows them. Example 3 — Iterator Insert Insert a character before begin.
#include <iostream>
#include <string>
int main() {
std::string s = "bc";
s.insert(s.begin(), 'a');
std::cout << s << '\n';
return 0;
} How It Works begin() points before 'b'.Inserting 'a' puts it first. String becomes "abc". Frequently Asked Questions What does insert do? It places new characters at a position and shifts the old ones right.
What if the index is too large? Index overloads throw std::out_of_range when pos > size().
Does insert invalidate iterators? Yes, especially if reallocation happens or elements move.
Can I insert a whole string? Yes: s.insert(pos, other).
What is insert_range? C++23 member that inserts a whole range of characters at once.
Next: insert_range() Keep going through the string functions. Insert all characters from a range at a position (C++23).
insert_range() tutorial → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion