The issue occurs on line 23. I don't believe you get the resulting iterator from that operation (a C++11 feature, but an important one) which I believe ultimately leads to an invalid iterator.
Inserting each character from newVal one by one, however, works in C++03. Remember to get the resulting iterator from each std::insert operation.
Albatross, when I do sbeg = s.insert(sbeg, newVal.cbegin(), newVal.cend());, its gives me the following error :
Testing.cpp: In function ‘std::string replace_str(std::string, std::string, std::string)’:
Testing.cpp:23:10: error: no match for ‘operator=’ (operand types are ‘__gnu_cxx::__normal_iterator<char*, std::basic_string<char> >’ and ‘void’)
sbeg = s.insert(sbeg, newVal.cbegin(), newVal.cend());
The return type is void. That is why it cannot be assigned to a iterator variable.
Form 6 will do what Albatross said iterator insert (iterator p, char c);
This is all assuming you are using C++98 standards. If you are compiling with C++11 standards you should look at those forms on that reference link I gave.
@kevin I always compile with C++11, but can you tell me where it states that the range returns void, as on that link it says the following:
Return value
The signatures returning a reference to string, return *this.
Those returning an iterator, return an iterator pointing to the first character inserted.
Member type iterator is a random access iterator type that points to characters of the string.
When you look at the reference pages on here, at the top there is C++98, C++11, C++14. You can click those and see the different prototypes for the reference you are looking at. The prototypes tell what type the function returns. The explanation you quoted applys to all the different overloaded forms.