Removing characters from a string using erase and

2019-08-01 07:34发布

问题:

I found this solution on Stack Overflow and other forums for removing characters from a string. Say I wanted to remove the white spaces from a string I'd do:

currentLine.erase( std::remove( currentLine.begin(), currentLine.end(), ' ' ), currentLine.end() );

where currentLine is the name of the string.

This sort of thing appears to work for people but if I use it I get:

/local/yrq12edu/Desktop/Bens_C++_Utilities/simuPOPtoFASTA/simuPOP2FASTA.cpp|54|error: cannot convert 'std::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >}' to 'const char*' for argument '1' to 'int remove(const char*)'|

As a compile error. I think it's something to do with the iterator that is returned by the std::remove function not working with the erase method, but apparently it should work. How do I fix this?

回答1:

What headers have you included? It looks like the only std::remove that the compiler is seeing is the one in <cstdio> (perhaps included through <iostream> or one of the other iostream heaaders). If you include <algorithm>, you should find the right one.