C++ c++;使用STL从字符串中删除字符
我脑子里有个小屁:我想删除C++ c++;使用STL从字符串中删除字符,c++,stl,stdstring,C++,Stl,Stdstring,我脑子里有个小屁:我想删除std::string中换行符'\n'的所有实例。我更喜欢使用STL而不是手动、多嵌套的for循环;唯一的问题是我忘了怎么 将(…){std::string::remove_if(…);}工作?我是否需要使用std::for_each(…,…,std::string::remove_if(…)?还是需要其他东西?第一个想法:删除习惯用法: str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());
std::string
中换行符'\n'
的所有实例。我更喜欢使用STL而不是手动、多嵌套的for循环;唯一的问题是我忘了怎么
将(…){std::string::remove_if(…);}代码>工作?我是否需要使用
std::for_each(…,…,std::string::remove_if(…)代码>?还是需要其他东西?第一个想法:删除习惯用法:
str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());
如果您有Boost.Range,它的工作时间甚至更短:
#include <boost\range\algorithm_ext\erase.hpp>
boost::remove_erase(str, '\n');
#包括
boost::删除擦除(str,“\n”);
事实上,这很可能是你能得到的最好的。我需要使用#include
来编译它。也可以使用更窄的名称boost::range::remove\u erase
并通过更宽的includeboost/range/algorithm\u ext.hpp