Open In App

string insert() in C++

Last Updated : 26 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, the string insert() function is used to insert characters or a string at the given position of the string. For example,

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    string s = "Geeks";
    
    // Inserting another string at the friend
    // of s
    s.insert(s.size(), "forGeeks");
    
    cout << s;
    return 0;
}

Output
GeeksforGeeks

The string method is a member function of std::string class defined inside <string> header file. It is implemented in five forms:

C++
str.insert(pos, c);
str.insert(pos, num, c);
str.insert(pos, first, last);
str1.insert(pos, str2);
str1.insert(pos, str2, str_idx, str_num);

These 5 implementation can be used in the following ways:

Insert a Single Character

The string insert() method can be used to insert a single character at a given position of the string.

Syntax

C++
str.insert(pos, c);

Parameters:

  • str: String in which we have to insert the string.
  • pos: Iterator pointing to the position where we have to insert the character.
  • c: Character which we have to insert.

Return Value:

  • It will return an iterator pointing to the inserted character of the string.

Example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    string str("Geeksforeeks");

    // Inserts 'G' at position
    // str.begin() + 8
    str.insert(str.begin() + 8, 'G');
    cout << str;

    return 0;
}

Output
GeeksforGeeks

If the character is inserted in the middle of the string, all the characters to the right of the given index will be shifted one place to the right to make space for new characters.

Insert a Single Character Multiple Times

The string insert() method can also be used to insert a single character multiple time at the given position in the string.

Syntax

C++
str.insert(pos, num, c);

Parameter:

  • pos: Iterator or index to the position where we have to insert.
  • num: Number of times we have to insert the character.
  • c: Character which we have to insert.

Return Value:

  • It will return the original string after inserting the characters.

Example

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    string str("GeeksforGks");

    // Insert 2 occurrences of 'e'
    // starting from position 9
    str.insert(9, 2, 'e');
    cout << str;

    return 0;
}

Output
GeeksforGeeks

Insert Characters from the Given Range

We can insert the characters from the given range to our string using string insert() method. This range can be any STL container.

Syntax

C++
str.insert(pos, first, last);

Parameter:

  • pos: Iterator or index to the position where we have to insert.
  • first: Iterator pointing to the first character in the range.
  • last: Iterator pointing to the character just after the last character in the range.

Return Value:

  • It will return an iterator pointing to the first inserted character.

Example

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    string str1("GeeksGeeks");
    string str2("GforG");

    // Defining the range as str2.begin()
    // + 1 to str2.end() - 1
    auto first = str2.begin() + 1;
    auto last = str2.end() - 1;
     
    // Inserts at position 
    // str1.begin() + 5
    str1.insert(str1.begin() + 5, first, last);
    cout << str1;

    return 0;
}

Output
GeeksforGeeks

Insert a String

The string insert() method is used to insert the string at the specific position of the string. All the characters to the right of the given index will be shifted right to make space for new characters.

Syntax

C++
str1.insert(pos, str2);

Parameter:

  • str1: It is the name of the string in which we have to insert the string.
  • pos: It is the position where we have to insert.
  • str2: It is the name of the string from which we have to insert.

Return Value:

  • It will return the original string after inserting the string.

Example

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    string str1("Hello World! ");
    string str2("GeeksforGeeks ");

    // Inserts str2 in str1 starting
    // from 6th index of str1
    str1.insert(6, str2);
    cout << str1;

    return 0;
}

Output
Hello GeeksforGeeks World! 

Insert a Part of the String

The string insert() method can be used to insert the substring at any particular position.

Syntax

C++
str1.insert(pos, str2, str_idx, str_num);

Parameter:

  • str1: It is the name of the string in which we have to insert the string.
  • pos: It is the position where we have to insert.
  • str2: It is the name of the string from which we have to insert.
  • str_idx: It is the starting position of the string from which we have to insert.
  • str_num: It represent the length of the substring which we have to insert.

Return Value:

  • It will return the original string after inserting the string.

Example

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    string str1("Hello World! ");
    string str2("GeeksforGeeks ");

    // Inserts 6 characters from 
    // index number 8 of str2 at 
    // index number 6 of str1
    str1.insert(6, str2, 8, 6);
    cout << str1;

    return 0;
}

Output
Hello Geeks World! 

Article Tags :
Practice Tags :

Similar Reads