我不太熟悉那个特定的方法,但看起来replace_all_copy
只想要一个替换字符串而不是is_any_of
的结果。
通过对string algorithms我注意到,有一个正则表达式版本,也将工作中的其他选项扫视:
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/regex.hpp>
int main(int argc, char** argv) {
std::string someString = "abc.def-ghi";
std::cout << someString << std::endl;
std::string toReplace = "[.-]"; // character class that matches . and -
std::string replacement = " ";
std::string processedString =
boost::replace_all_regex_copy(someString, boost::regex(toReplace), replacement);
std::cout << processedString << std::endl;
return 0;
}
输出:
abc.def-ghi
abc def ghi
这确实需要链接到的升压正则表达式库。就我而言,我建:
g++ -L/usr/local/Cellar/boost/1.52.0/lib -lboost_regex-mt main.cpp
我也在看,以及我不确定复制部分对于原始问题的重要性。基于文档,它看起来像someString将被修改,并返回一个引用。如果复制不重要,我更喜欢你的解决方案,因为你避免使用正则表达式。 –
@JesseVogt好点,我更新了答案。 – jrok
酷 - 不知道我完全错过了看文档中的replace_copy_if。好的解决方案 –