#include#include using namespace std; int main() { string str = "Hello world"; str.insert(5, "beautiful "); cout << str << endl; // Hello beautiful world return 0; }
#includeIn this example, we use iterators to specify the position where the substring should be inserted. We first declare a string `sub` with the value "beautiful ". We then use the `begin()` and `end()` functions of both `str` and `sub` to specify the range where `sub` should be inserted, starting from position 5 of `str`. The resulting string is "Hello beautiful world". The package library used in this code is the Standard Template Library (STL).#include using namespace std; int main() { string str = "Hello world"; string sub = "beautiful "; str.insert(str.begin() + 5, sub.begin(), sub.end()); cout << str << endl; // Hello beautiful world return 0; }