【发布时间】:2013-03-19 09:29:52
【问题描述】:
我正在使用 boost::split 来解析数据文件。数据文件包含如下行。
数据.txt
1:1~15 ASTKGPSVFPLAPSS SVFPLAPSS -12.6 98.3
项目之间的空白是制表符。我要拆分以上行的代码如下。
std::string buf;
/*Assign the line from the file to buf*/
std::vector<std::string> dataLine;
boost::split( dataLine, buf , boost::is_any_of("\t "), boost::token_compress_on); //Split data line
cout << dataLine.size() << endl;
对于上面的代码行,我应该打印出 5,但我得到了 6。我试图通读文档,这个解决方案似乎应该做我想做的事,显然我遗漏了一些东西。谢谢!
编辑: 在 dataLine 上运行如下 forloop 会得到以下结果。
cout << "****" << endl;
for(int i = 0 ; i < dataLine.size() ; i ++) cout << dataLine[i] << endl;
cout << "****" << endl;
****
1:1~15
ASTKGPSVFPLAPSS
SVFPLAPSS
-12.6
98.3
****
【问题讨论】:
-
dataLine 中存储的值是什么?
-
I get 5,您的
buf包含其他内容。 -
也许它没有正确复制到此页面中,您将其错误地复制到了测试代码中。让我看看如何确保它正确复制。
-
如果你
buf末尾有空格,I get the same results。 -
仅使用
boost::algorithm::trim变体是否不够?