Boost_tokenizer

1
#include< boost/tokenizer.hpp>
  1. split string: default space

    1
    2
    3
    4
    5
    6
    string s = "This is,  a test";  
    tokenizer<> tok(s);
    for(tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg)
    {
    cout << *beg << "\n";
    }
  2. split string: drop delimiter

    1
    2
    3
    typedef boost::tokenizer<boost::char_separator<char>>  tokenizer;
    boost::char_separator< char> sep("-;|");
    tokenizer tokens(str, sep);
  3. split string: drop delimiter and keep delimiter

    1
    2
    3
    typedef boost::tokenizer< boost::char_separator< char> > tokenizer;
    boost::char_separator< char> sep("-;", "|", boost::keep_empty_tokens);
    tokenizer tokens(str, sep);
  4. special kinds of tokenizer

    1
    2
    3
    int offsets[] = {2,2,4};   //three segment length  
    offset_separator f(offsets, offsets+3);
    tokenizer< offset_separator> tok(s,f);