VOOZH about

URL: https://www.geeksforgeeks.org/cpp/stdstringinsert-in-c/

⇱ string insert() in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

string insert() in C++

Last Updated : 26 May, 2025

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


Output
GeeksforGeeks

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

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

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:


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

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


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

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


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

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


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

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


Output
Hello Geeks World! 
Comment