insert() is used to insert characters in string at specified position. It supports various syntaxes to facilitate same, here we will describe them.
Syntax 1: Inserts the characters of str starting from index idx.
string& string::insert (size_type idx, const string& str) idx :is the index number str : is the string from which characters is to be picked to insert Returns *this. Errors: Throws out_of_range if idx > size(). Throws length_error if the resulting size exceeds the maximum number of characters.
// CPP code for insert (size_type idx, const string& str) #include <iostream> #include <string> using namespace std;
// Function to demonstrate insert void insertDemo(string str1, string str2)
{ // Inserts str2 in str1 starting
// from 6th index of str1
str1.insert(6, str2);
cout << "Using insert : " ;
cout << str1;
} // Driver code int main()
{ string str1( "Hello World! " );
string str2( "GeeksforGeeks " );
cout << "Original String : " << str1 << endl;
insertDemo(str1, str2);
return 0;
} |
Output:
Original String : Hello World! Using insert : Hello GeeksforGeeks World!
Syntax 2: Inserts at most, str_num characters of str, starting with index str_idx.
string& string::insert (size_type idx, const string& str, size_type str_idx, size_type str_num) idx : is the index number where insertion is to be made. str : is the string from which characters are to be picked to insert. str_idx : is the index number in str. str_num : is the number of characters to be inserted from str. Returns *this. Errors: Throws out_of_range if idx > size(). Throws out_of_range if str_idx > str.size(). Throws length_error if the resulting size exceeds the maximum number of characters.
// CPP code for insert (size_type idx, const string& str, // size_type str_idx, size_type str_num) #include <iostream> #include <string> using namespace std;
// Function to demonstrate insert void insertDemo(string str1, string str2)
{ // Inserts 6 characters from index number
// 8 of str2 at index number 6 of str1
str1.insert(6, str2, 8, 6);
cout << "Using insert : " ;
cout << str1;
} // Driver code int main()
{ string str1( "Hello World! " );
string str2( "GeeksforGeeks " );
cout << "Original String : " << str1 << endl;
insertDemo(str1, str2);
return 0;
} |
Output:
Original String : Hello World! Using insert : Hello Geeks World!
Syntax 3: Inserts the characters of the C-string cstr so that the new characters start with index idx.
string& string::insert (size_ type idx, const char* cstr) idx : is the index number where insertion is to be made. *cstr : is the pointer to the C-string which is to be inserted. Returns *this. Errors: Throws out_of_range if idx > size(). Throws length_error if the resulting size exceeds the maximum number of characters.
Note: cstr may not be a null pointer (NULL).
// CPP code for insert(size_ type idx, const char* cstr) #include <iostream> #include <string> using namespace std;
// Function to demonstrate insert void insertDemo(string str)
{ // Inserts " are " at 5th index of str
str.insert(5, " are " );
cout << "Using insert : " ;
cout << str;
} // Driver code int main()
{ string str( "GeeksforGeeks " );
cout << "Original String : " << str << endl;
insertDemo(str);
return 0;
} |
Output:
Original String : GeeksforGeeks Using insert : Geeks are forGeeks
Syntax 4: Inserts chars_len characters of the character array chars so that the new characters start with index idx.
string& string::insert (size_type idx, const char* chars, size_type chars_len) idx : index number where insertion is to be made. *chars : is the pointer to the array. chars_len : is the number of characters to be inserted from character array. Returns : *this Errors: Throws out_of_range if idx > size(). Throws length_error if the resulting size exceeds the maximum number of characters.
Note : chars must have at least chars_len characters.
// CPP code for insert (size_type idx, const char* chars, // size_type chars_len) #include <iostream> #include <string> using namespace std;
// Function to demonstrate insert void insertDemo(string str)
{ // Inserts 10 characters from" are here "
// at 5th index of str
str.insert(5, " are here " , 10);
cout << "Using insert : " ;
cout << str;
} // Driver code int main()
{ string str( "GeeksforGeeks " );
cout << "Original String : " << str << endl;
insertDemo(str);
return 0;
} |
Output:
Original String : GeeksforGeeks Using insert : Geeks are here forGeeks
Syntax 5: Inserts num occurrences of character c at the position specified by idx.
string& string ::insert (size_type idx, size_type num, char c) idx : is the index number where insertion is to be made. c : is the character to be inserted. num : is the number of repetition of character c Returns : *this Errors: Throw out_of_range if idx > size(). Throw length_error if the resulting size exceeds the maximum number of characters.
// CPP code for :insert (size_type idx, size_type num, char c) #include <iostream> #include <string> using namespace std;
// Function to demonstrate insert void insertDemo(string str)
{ // Inserts at 5th index,
// 5 occurrences of '$'
str.insert(5, 5, '$' );
cout << "Using insert : " ;
cout << str;
} // Driver code int main()
{ string str( "**********" );
cout << "Original String : " << str << endl;
insertDemo(str);
return 0;
} |
Output:
Original String : ********** Using insert : *****$$$$$*****
Syntax 6: Inserts num occurrences of character c at the position specified by iterator pos.
void string ::insert (iterator pos, size_type num, char c) pos : is the position of iterator. c : is the character which is to be inserted. Returns : *this Errors: Throws out_of_range if pos > size(). Throws length_error if the resulting size exceeds the maximum number of characters.
// CPP code for :insert (iterator pos, size_type num, char c) #include <iostream> #include <string> using namespace std;
// Function to demonstrate insert void insertDemo(string str)
{ // Inserts 5 occurrences of '$'
// at position str.begin() + 5
str.insert(str.begin() + 5, 5, '$' );
cout << "Using insert : " ;
cout << str;
} // Driver code int main()
{ string str( "**********" );
cout << "Original String : " << str << endl;
insertDemo(str);
return 0;
} |
Output:
Original String : ********** Using insert : *****$$$$$*****
Syntax 7: Inserts a copy of character c before the character to which iterator pos refers.
iterator string ::insert (iterator pos, char c ) pos : is the position of iterator. c : is the character which is to be inserted. Returns : iterator pointing to the first character inserted. Error: Throws length_error if the resulting size exceeds the maximum number of characters.
// CPP code for :insert (iterator pos, char c ) #include <iostream> #include <string> using namespace std;
// Function to demonstrate insert void insertDemo(string str)
{ std::string::iterator pos;
// Inserts '$' at position
// str.begin() + 5
pos = str.insert(str.begin()+5, '$' );
cout << "Using insert : " ;
cout << str << endl;
cout << "Value at Iterator returned : " << *pos;
} // Driver code int main()
{ string str( "**********" );
cout << "Original String : " << str << endl;
insertDemo(str);
return 0;
} |
Output:
Original String : ********** Using insert : *****$***** Value at Iterator returned : $
Syntax 8: Inserts all characters of the range [ beg,end ) before the character to which iterator pos refers.
void string ::insert (iterator pos, InputIterator beg, InputIterator end ) pos : is the iterator position. beg, end : Input iterators to the initial and final positions in a sequence. Error: Throws length_error if the resulting size exceeds the maximum number of characters.
// CPP code for insertinsert (iterator pos, InputIterator beg, // InputIterator end ) #include <iostream> #include <string> using namespace std;
// Function to demonstrate insert void insertDemo(string str1, string str2)
{ // Inserts str2.begin() + 5 , str2.end() - 6
// at position str1.begin() + 6
str1.insert(str1.begin() + 6, str2.begin() + 5 , str2.end() - 6);
cout << "Using insert : " ;
cout << str1;
} // Driver code int main()
{ string str1( "Hello World! " );
string str2( "GeeksforGeeks " );
cout << "Original String : " << str1 << endl;
insertDemo(str1, str2);
return 0;
} |
Output:
Original String : Hello World! Using insert : Hello forWorld!
If you like GeeksforGeeks(We know you do!) and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org.