Removing characters from a string using erase and remove

别说谁变了你拦得住时间么 提交于 2019-12-11 06:09:39

问题


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.



来源:https://stackoverflow.com/questions/21914485/removing-characters-from-a-string-using-erase-and-remove

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!