Boost :: algorithm - line splitting returns extra token

Maybe someone can tell me what's going on here?

My intention is to break the input string into curly braces: ie: ( 'or ) .

For the input line "(good) hello (there) of the world" I expect 4 tokens to be returned: good; Hello; there; peace.

As you can see from my sample application below, I get 5 tokens back (the first is an empty string).

Is there any way to get this to return only non-empty lines to me ?

#include <iostream> #include <boost/algorithm/string.hpp> #include <vector> int main() { std::string in = "(well)hello(there)world"; std::vector<std::string> tokens; boost::split(tokens, in, boost::is_any_of("()")); for (auto s : tokens) std::cout << "\"" << s << "\"" << std::endl; return 0; } 

Output:

 $ a.out "" <-- where is this token coming from? "well" "hello" "there" "world" 

I tried using boost::algorithm::token_compress_on but I get the same result.

+4
source share
2 answers

Yes, the first result returned is the empty set {} immediately preceding the first open bracket. The behavior is as expected.

If you do not want to use this result, just check the empty return variable and discard it.

To check that this is the expected behavior, put a bracket at the end of the line and you will get another empty result at the end. :)

+1
source

this thread is kind of old, but this is the best solution to boost::token_compress_on , add this after the delimeter to boost::split

0
source

Source: https://habr.com/ru/post/1436504/


All Articles