>_replace
This invades the implementation's namespace. Leading underscores are reserved for internal implementation names. You also need to be more specific about what you're trying to do with this function. The name suggests that you want to replace the contents of s1 with the contents of s2, an operation that is supported in a number of ways by the string class to begin with. Here is one of them:
Code:
#include <iostream>
#include <string>
using namespace std;
void replace_string ( string& to_place, const string& be_placed );
int main()
{
string s1 = "thisisastring";
string s2 = "thisisanother";
cout<< s1 <<" -- "<< s2 <<endl;
replace_string ( s1, s2 );
cout<< s1 <<" -- "<< s2 <<endl;
return 0;
}
void replace_string ( string& to_place, const string& be_placed )
{
to_place.assign ( be_placed );
}
On a side note, the whole diff thing is just going to confuse the matter. If you're worried about walking outside of s1's boundaries (a valid concern!), why not just do something like this if you feel the need to perform the replacement manually:
Code:
#include <iostream>
#include <string>
using namespace std;
void replace_string ( string& to_place, const string& be_placed );
int main()
{
string s1 = "thisisastring";
string s2 = "thisisanothertest";
cout<< s1 <<" -- "<< s2 <<endl;
replace_string ( s1, s2 );
cout<< s1 <<" -- "<< s2 <<endl;
return 0;
}
void replace_string ( string& to_place, const string& be_placed )
{
const string::size_type end = to_place.size();
for ( string::size_type i = 0; i != end; ++i )
to_place[i] = be_placed[i];
}