且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

的boost ::标记生成逗号分隔(C ++)

更新时间:2023-01-20 08:08:31

您必须给分隔标记生成器!

You must give the separator to tokenizer!

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

此外,更换德precated char_delimiters_separator与char_delimiter:

Also, replace the deprecated char_delimiters_separator with char_delimiter:

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复杂类型:所以最终的版本可能是:

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";
}