c – boost :: algorithm – 拆分字符串会返回一个额外的令牌
发布时间:2020-12-16 07:01:32 所属栏目:百科 来源:网络整理
导读:也许有人可以告诉我这里发生了什么? 我的意图是在大括号上分割输入字符串:即:'(‘或’)’. 对于输入字符串“(well)hello(there)world”,我希望返回4个令牌:well;你好;那里;世界. 从下面我的示例应用程序中可以看到,我得到5个令牌(第1个是空字符串). 有没
也许有人可以告诉我这里发生了什么?
我的意图是在大括号上分割输入字符串:即:'(‘或’)’. 对于输入字符串“(well)hello(there)world”,我希望返回4个令牌:well;你好;那里;世界. 从下面我的示例应用程序中可以看到,我得到5个令牌(第1个是空字符串). 有没有办法让这个只返回非空字符串? #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,但得到了相同的结果. 解决方法
是的,返回的第一个结果是紧接在第一个左括号之前的空集{}.行为符合预期.
如果您不想使用该结果,只需测试一个空的返回变量并将其丢弃. 要测试这是预期的行为,请在行的末尾添加一个括号,并在结尾处输入另一个空结果. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |