如果你的字符串是"c:\x\y\z",你的字符串中没有\.\表示转义字符.将您的字符串更改为"c:\\x\\y\\z".
另外,请注意如何replace工作 - http://www.cplusplus.com/reference/string/string/replace/
我不认为你可以直接'\\'用两个字符替换一个字符"//".(我可能被证明是错的).
替代方案:
std::stringstream ss;
for ( int i = 0 ; i < str.size() ; i++ )
{
if ( str[i] == '\\' )
ss << "//";
else
ss << str[i];
}
str = ss.str();
Run Code Online (Sandbox Code Playgroud)