回答編集履歴

2 No need to fill s[j] by '\0'.

majiponi

majiponi score 905

2016/05/09 11:47  投稿

How about this?
```C++
void remove_crlf(std::string& s)
{
   size_t i, j;
   for( i = 0, j = 0; i < s.size(); i++ ){
       if( s[i] != '\r' && s[i] != '\n' ){
           s[j] = s[i];
           j++;
       }
   }
   s[j] = '\0';  
   s.resize(j);
}
```
```
Without resizing, this string is not null-terminating.
1 Fixed to be null-terminated string.

majiponi

majiponi score 905

2016/05/09 11:43  投稿

How about this?
```C++
void remove_crlf(std::string& s)
{
   size_t i, j;
   for( i = 0, j = 0; i < s.size(); i++ ){
       if( s[i] != '\r' && s[i] != '\n' ){
           s[j] = s[i];
           j++;
       }
   }
   s[j] = '\0';  
   s.resize(j);
}
```

思考するエンジニアのためのQ&Aサイト「teratail」について詳しく知る