Boost::tokenizer comma separated (c++)

半腔热情 提交于 2019-12-03 14:19:34
CapelliC

You must give the separator to tokenizer!

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

Also, replace the deprecated char_delimiters_separator with 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";
}

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:

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";
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!