【发布时间】:2012-09-27 01:49:55
【问题描述】:
也许有人可以告诉我这里发生了什么?
我的意图是在大括号上拆分输入字符串:即:'(' 或 ')'。
对于 "(well)hello(there)world" 的输入字符串,我希望返回 4 个标记:well;你好;那里;世界。
从下面的示例应用程序中可以看出,我得到了 5 个令牌(第一个是空字符串)。
有没有办法让这个只返回非空字符串?
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <vector>
int main()
{
std::string in = "(well)hello(there)world";
std::vector<std::string> tokens;
boost::split(tokens, in, boost::is_any_of("()"));
for (auto s : tokens)
std::cout << "\"" << s << "\"" << std::endl;
return 0;
}
输出:
$ a.out
"" <-- where is this token coming from?
"well"
"hello"
"there"
"world"
我尝试过使用boost::algorithm::token_compress_on,但得到了相同的结果。
【问题讨论】:
-
只是好奇,尝试删除
well之前的(。在第一个(之前,空字符串可能没有任何意义。