C++ std::string insert() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::string

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.

C++
s.insert(2, "XX");
s.insert(s.begin(), '!');
s.insert(0, 3, '*');
CallWhat 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
C++
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

ParameterDescription
posIndex or iterator where insertion begins.
str / ch / nText or repeated character to insert.

Return Value

Return ValueDescription
string& or iteratorIndex overloads return *this; iterator overloads return an iterator to the first inserted character.

C++ Examples

Example 1 — Basic insert()

Insert text at index 2.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "CFun";
    s.insert(1, "odeTo");
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. Index 1 sits before 'F' in "CFun".
  2. Inserting "odeTo" shifts the rest right.
  3. Result is "CodeToFun".

Example 2 — Insert Repeated Chars

Insert three stars at the front.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "go";
    s.insert(0, 3, '*');
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. Position 0 is the front.
  2. Three '*' characters are inserted.
  3. Original text follows them.

Example 3 — Iterator Insert

Insert a character before begin.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "bc";
    s.insert(s.begin(), 'a');
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. begin() points before 'b'.
  2. Inserting 'a' puts it first.
  3. String becomes "abc".

Frequently Asked Questions

It places new characters at a position and shifts the old ones right.
Index overloads throw std::out_of_range when pos > size().
Yes, especially if reallocation happens or elements move.
Yes: s.insert(pos, other).
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.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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