gpt4 book ai didi

c++ - Boost::tokenizer 逗号分隔 (c++)

转载 作者:可可西里 更新时间:2023-11-01 17:32:56 24 4
gpt4 key购买 nike

对你们来说应该是一个简单的......

我正在玩弄使用 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

更新

最佳答案

您必须将分隔符提供给分词器!

boost::tokenizer<boost::char_delimiters_separator<char>>tok(s, sep);

此外,将已弃用的 char_delimiters_separator 替换为 char_separator:

string s = "this is, , ,  a test";
boost::char_separator<char> sep(",");
boost::tokenizer< boost::char_separator<char> > tok(s, sep);
for(boost::tokenizer< boost::char_separator<char> >::iterator beg = tok.begin(); beg != tok.end(); ++beg)
{
cout << *beg << "\n";
}

请注意,还有一个模板参数不匹配:typedef 此类复杂类型是个好习惯:所以最终版本可能是:

string s = "this is, , ,  a test";
boost::char_separator<char> sep(",");
typedef boost::tokenizer< boost::char_separator<char> > t_tokenizer;
t_tokenizer tok(s, sep);
for (t_tokenizer::iterator beg = tok.begin(); beg != tok.end(); ++beg)
{
cout << *beg << "\n";
}

关于c++ - Boost::tokenizer 逗号分隔 (c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7941725/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com