也许有人可以告诉我这里发生了什么?
我的意图是在大括号上拆分输入字符串:即:'(' 或 ')'。
对于 "(well)hello(there)world" 的输入字符串,我希望返回 4 个标记:well;你好;那里;世界。
正如您从下面我的示例应用程序中看到的那样,我得到了 5 个 token (第一个是空字符串)。
有什么方法可以让它只返回非空字符串吗?
#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
但我得到了相同的结果。
最佳答案
是的,返回的第一个结果是第一个左括号前的空集 {}。行为符合预期。
如果您不想使用该结果,只需测试一个空的返回变量并将其丢弃。
要测试这是预期的行为,请在该行的末尾放置一个括号,您将在末尾得到另一个空结果。 :)
关于c++ - boost::algorithm - 拆分字符串返回一个额外的标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12612965/