我有一个小脑袋:我想在std::string
中删除换行符'\n'
的所有实例。我宁愿使用STL而不是手动,多嵌套for循环;唯一的问题是我忘了如何...C++使用STL从字符串中删除字符
会for(...) { std::string::remove_if(...); } ;
工作?我需要使用std::for_each(...,..., std::string::remove_if(...));
吗?或者会需要其他东西?
我有一个小脑袋:我想在std::string
中删除换行符'\n'
的所有实例。我宁愿使用STL而不是手动,多嵌套for循环;唯一的问题是我忘了如何...C++使用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 :: range :: remove_erase'下并通过更广泛的'boost/range/algorithm_ext.hpp' – Stein
+1为了提高效率:) – Mahesh
事实上,这是最有可能你会得到最好的。 – chris
我需要使用'#include'来编译。 –
Westy92