【发布时间】:2011-08-09 17:02:38
【问题描述】:
我正在使用boost::split 方法将字符串拆分为:
我首先确保包含正确的标题以访问boost::split:
#include <boost/algorithm/string.hpp>
然后:
vector<string> strs;
boost::split(strs,line,boost::is_any_of("\t"));
线条就像
"test test2 test3"
这就是我使用结果字符串向量的方式:
void printstrs(vector<string> strs)
{
for(vector<string>::iterator it = strs.begin();it!=strs.end();++it)
{
cout << *it << "-------";
}
cout << endl;
}
但是为什么在结果strs我只得到"test2"和"test3",不应该是"test"、"test2"和"test3",字符串中有\t(制表符)。
2011 年 4 月 24 日更新: 似乎在我更改 printstrs 的一行代码后,我可以看到第一个字符串。我变了
cout << *it << "-------";
到
cout << *it << endl;
似乎"-------" 以某种方式覆盖了第一个字符串。
【问题讨论】:
-
展示你如何使用向量。我猜问题就在那里。
-
boost::is_any_of("\t")的效率不如[](char c) { return c=='\t';}。您只想检查一种可能性。 (不知道为什么没有boost:is('\t')) -
@MSalters 注释中的代码是什么意思?如何使用该代码替换
boost::is_any_of()? -
@PoscoGrubb:它被称为“lambda”,
boost::splits理解它们。