How to use boost split to split a string and ignore empty values?

前端 未结 3 1717
遇见更好的自我
遇见更好的自我 2021-02-04 02:55

I am using boost::split to parse a data file. The data file contains lines such as the following.

data.txt

1:1~15  ASTKGPSVFPLAPSS SVFPLAPSS   -12.6   98         


        
3条回答
  •  无人共我
    2021-02-04 03:44

    Even though "adjacent separators are merged together", it seems like the trailing delimeters make the problem, since even when they are treated as one, it still is one delimeter.

    So your problem cannot be solved with split() alone. But luckily Boost String Algo has trim() and trim_if(), which strip whitespace or delimeters from beginning and end of a string. So just call trim() on buf, like this:

    std::string buf = "1:1~15  ASTKGPSVFPLAPSS SVFPLAPSS   -12.6   98.3    ";
    std::vector dataLine;
    boost::trim_if(buf, boost::is_any_of("\t ")); // could also use plain boost::trim
    boost::split(dataLine, buf, boost::is_any_of("\t "), boost::token_compress_on);
    std::cout << out.size() << std::endl;
    

    This question was already asked: boost::split leaves empty tokens at the beginning and end of string - is this desired behaviour?

提交回复
热议问题