1

I tried using the std::replace algorithm:

replace(out.begin(), out.end(), '\r', '\r\n');    // error
replace(out.begin(), out.end(), "\r", "\r\n");    // error
replace(out.begin(), out.end(), "\\r", "\\r\\n"); // error

I always get errors that the parameters are ambigous. How exactly can I specify the \r and \n so the compiler will not complain ?

edit:

errors:

 could not deduce template argument for 'const _Ty &' from 'const char [5]' 
 template parameter '_Ty' is ambiguous
'replace': no matching overloaded function found    
Adrian
  • 19,440
  • 34
  • 112
  • 219

1 Answers1

3

While in principle this might be solved with some combination of standard/Boost functions, it's specific enough to get its own function, and thus its own impl as well. Which could be as simple as this:

std::string cr_to_crlf(std::string const& s) {
    std::string result;
    result.reserve(s.size());

    for (char c : s) {
        result += c;
        if (c == '\r') {
            result += '\n';
        }
    }
    return result;
}
Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135