boost::algorithm - 拆分字符串会返回一个额外的标记
boost::algorithm - splitting a string returns an extra token
也许有人可以告诉我这里发生了什么?
我的目的是在大括号上拆分输入字符串:即:"("或")"。
对于"(well)hello(there)world"的输入字符串,我希望返回4个标记:well; hello; there; world。
正如您从下面的示例应用程序中看到的那样,我得到了 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
但我得到相同的结果。
是的,返回的第一个结果是紧接在第一个左括号之前的空集 {}。行为符合预期。
如果您不想使用该结果,只需测试返回的空变量并将其丢弃即可。
若要测试这是预期行为,请在行尾加上一个括号,最后将得到另一个空结果。 :)
这个线程有点旧,但这是更好的解决方案boost::token_compress_on
,在boost::split
的分律表之后添加这个
相关文章:
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 给定一个大小为 N 的数组 S,检查是否可以将序列拆分为两个序列
- 使用连续分隔符和空最后一个字符进行拆分
- 如何在C++中将一个值拆分为一个十进制整数
- 如何将一个单词拆分成字母,并将它们放入一个无序的列表/集合中
- 如何根据另一个向量中给出的索引拆分向量?
- 将一个数字拆分为多个数字,每个数字只有一个有效位
- 为什么最后一个拆分函数会导致分割故障?如何解决此问题
- 如何用Arduino中的另一个字符串将字符串拆分
- 如何在C++中将一个简单的类似 Lisp 的代码拆分为标记?
- 如何将文本(诗歌)拆分为行(字符串/字符[])并查找每个行的最后一个单词
- Arduino - 将拆分字符串与另一个字符串进行比较
- C++ ifstream 拆分 w.r.t. 一个角色
- boost::algorithm - 拆分字符串会返回一个额外的标记
- 如何将XML文件拆分为多个XML,以便每个新文件只包含原始文件中的一个文本节点
- 将一个大型c++程序拆分为几个组件
- 将一个std::数组拆分为一个较小大小的std::array元组
- Qt-拆分一个QString,使用几种类型的空白作为分隔符
- C++:解析拆分但一个条目可以包含多行的日志
- c++ 如何根据最后一个字符串将字符串拆分为两个字符串'.'