Search code examples
c++boosttokenizeboost-tokenizer

Boost::tokenizer comma separated (c++)


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


Solution

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