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,
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;
}
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 CharacterThe string insert() method can be used to insert a single character at a given position of the string.
Syntax C++
Parameters:
Return Value:
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;
}
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 TimesThe string insert() method can also be used to insert a single character multiple time at the given position in the string.
Syntax C++
Parameter:
Return Value:
#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;
}
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:
Return Value:
#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;
}
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++
Parameter:
Return Value:
#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;
}
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:
Return Value:
#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;
}
Hello Geeks World!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4