Boost::tokenizer 逗号分隔 (c++)
对你们来说应该是一个简单的......
我正在使用 Boost 玩标记器,我想创建一个以逗号分隔的标记。这是我的代码:
string s = "this is, , , a test";
boost::char_delimiters_separator<char> sep(",");
boost::tokenizer<boost::char_delimiters_separator<char>>tok(s, sep);
for(boost::tokenizer<>::iterator beg= tok.begin(); beg!=tok.end(); ++beg)
{
cout << *beg << "\n";
}
我想要的输出是:
This is
a test
我得到的是:
This
is
,
,
,
a
test
更新
Should be an easy one for you guys.....
I'm playing around with tokenizers using Boost and I want create a token that is comma separated. here is my code:
string s = "this is, , , a test";
boost::char_delimiters_separator<char> sep(",");
boost::tokenizer<boost::char_delimiters_separator<char>>tok(s, sep);
for(boost::tokenizer<>::iterator beg= tok.begin(); beg!=tok.end(); ++beg)
{
cout << *beg << "\n";
}
The output that I want is:
This is
a test
What I am getting is:
This
is
,
,
,
a
test
UPDATED
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须将分隔符提供给分词器!
另外,将已弃用的 char_delimiters_separator 替换为 char_separator:
请注意,还有一个模板参数不匹配的情况: typedef 此类复杂类型是一个好习惯:因此最终版本可能是:
You must give the separator to tokenizer!
Also, replace the deprecated char_delimiters_separator with char_separator:
Please note that there is also a template parameter mismatch: it's good habit to typedef such complex types: so the final version could be:
使用现代 C++,可以使用比其他答案提供的更简单的公式:
With modern C++, a much simpler formulation is possible than the other answer provides: