【发布时间】:2016-08-15 07:17:38
【问题描述】:
我想通过and 的任何出现来拆分字符串。
首先我要明确一点,我不打算使用任何regex作为分隔符。
我运行以下代码:
#include <iostream>
#include <regex>
#include <boost/algorithm/string.hpp>
int main()
{
std::vector<std::string> results;
std::string text=
"Alexievich, Svetlana and Lindahl,Tomas and Campbell,William";
boost::split(
results,
text,
boost::is_any_of(" and "),
boost::token_compress_off
);
for(auto result:results)
{
std::cout<<result<<"\n";
}
return 0;
}
结果和我预期的不一样:
Alexievich,
Svetl
Li
hl,Tom
s
C
mpbell,Willi
m
似乎分隔符中的每个字符都是单独作用的,而我需要将整个 and 作为分隔符。
请不要链接到this boost example,除非您确定它适用于我的情况。
【问题讨论】:
-
这就是
is_any_of的意思,它匹配字符串中的任何 个字符。第三个参数是一个 predicate,这意味着它可以是任何可调用对象,包括 lambda。另一个问题是boost::split似乎是基于字符,而不是基于“单词”。 -
@JoachimPileborg,感谢您的评论。用什么代替?