I believe the error is because the type of "I"
is const char[2]
and the type of "you"
is const char[4]
and not the char*
you may be expecting.
The array decays to the pointer when required, but the template type deduction doesn't do that automatically.
Bear in mind as well, that with std::replace
you will want to replace individual elements in the original string, so only use char
.
A simply alternative snippet of code to replace the "I"
with "you"
is;
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
std::string inStr = "I go, I walk, I climb";
//std::replace( inStr.begin(), inStr.end(), "I", "you");
auto at = inStr.find("I");
while (at < inStr.size()) {
inStr.replace(at, 1, "you");
at = inStr.find("I");
}
cout << inStr << endl;
}
You can use the MS online compiler here to verify the code.