-2

I've tried:

versionString = versionString.replace(versionString.begin(), versionString.end(), '(' , '-' );

The result is: "--------------". Basically replacing all the characters. What is that?

versionString is a basic string.

exigez
  • 41
  • 4
  • 1
    Might have something to do with the fact that `std::string` [has no overload](https://en.cppreference.com/w/cpp/string/basic_string/replace) of `replace` that accepts two characters. Unless of course you aren't using `std::string`, in which case you need to provide one hella better [mcve]. – StoryTeller - Unslander Monica Jan 30 '19 at 15:00
  • 1
    http://www.cplusplus.com/reference/string/string/replace/ – Mikołaj Mularczyk Jan 30 '19 at 15:00
  • This one has tripped me up more than once, as it doesn't do the same thing as [`std::replace`](https://en.cppreference.com/w/cpp/algorithm/replace) at all. (One thing I've learned is to always be cautious around `std::string` and assume that my assumptions are probably wrong.) – molbdnilo Jan 30 '19 at 15:15

1 Answers1

1

If you look at e.g. this std::string::replace reference you will see that there's no overload that takes the arguments you pass. Something the compiler really should warn you about.

The closes one is number 6:

basic_string& replace( const_iterator first, const_iterator last,
                       size_type count2, CharT ch );

which replaces the range with count2 copies of ch.

That is, you replace your string with '(' number of dashes. With ASCII that '(' will be converted to the integer 40 (it's this conversion the compiler should have warned you about).

One solution is to repeatedly find the character you want to replace, and replace only that single character.

A much simpler solution is to use the standard algorithm function std::replace:

std::replace(begin(versionString), end(versionString), '(', '-');
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621