Rella
Rella

Reputation: 66945

C++ how to turn all `\` into `/` inside of string?

So I try std::replace(diff_path.begin(), diff_path.end(), "\\", "/"); but it seems not to compile on my visual studio. What to do - how to turn all \ into / inside of string?

Error   3   error C2446: '==' : no conversion from 'const char *' to 'int'  c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    1311    1   CloudServerPrototype

Error   5   error C2440: '=' : cannot convert from 'const char [2]' to 'char'   c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    1312    1   CloudServerPrototype    

Error   4   error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'    c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    1311    1   CloudServerPrototype

Upvotes: 0

Views: 159

Answers (2)

Hans Passant
Hans Passant

Reputation: 941585

You did it right in your question title. Fix:

 std::replace(diff_path.begin(), diff_path.end(), '\\', '/');

Elements of an std::string are characters, not strings.

Upvotes: 4

James McNellis
James McNellis

Reputation: 355079

You need to use character literals, not string literals:

std::replace(diff_path.begin(), diff_path.end(), '\\', '/');
                                                 ^~~~  ^~~

The value_type of a std::string is char (each element in a string is a single character).

Upvotes: 9

Related Questions