c++ - Boost::tokenizer point separated, but also keeping empty fields -
i have seen this question , mine similar it, different, please not mark duplicate.
my question is: how empty fields string?
i have string std::string s = "this.is..a.test"; , want fields <this> <is> <> <a> <test>.
i have tried
typedef boost::char_separator<char> chsep; typedef boost::tokenizer<chsep> tknchsep; chsep sep(".", ".", boost::keep_empty_tokens); tknchsep tok(s, sep); (tknchsep::iterator beg = tok.begin(); beg != tok.end(); ++beg) { std::cout << "<" << *beg << "> "; } but <this> <.> <is> <.> <> <.> <a> <test>.
the second argument boost.tokenizer's char_separator kept_delims parameter. used specify delimiters show tokens. original code specifying "." should kept token. resolve this, change:
chsep sep(".", ".", boost::keep_empty_tokens); to:
chsep sep(".", "", boost::keep_empty_tokens); // ^-- no delimiters show tokens. here complete example:
#include <iostream> #include <string> #include <boost/foreach.hpp> #include <boost/tokenizer.hpp> int main() { std::string str = "this.is..a.test"; typedef boost::tokenizer<boost::char_separator<char> > tokenizer; boost::char_separator<char> sep( ".", // dropped delimiters "", // kept delimiters boost::keep_empty_tokens); // empty token policy boost_foreach(std::string token, tokenizer(str, sep)) { std::cout << "<" << token << "> "; } std::cout << std::endl; } which produces desired output:
<this> <is> <> <a> <test>
Comments
Post a Comment