boost::split leaves empty tokens at the beginning and end of string - is this desired behaviour?

前端 未结 3 754
暗喜
暗喜 2021-01-02 06:32

Since I couldn\'t find anything on this in the documentation, I thought I ask it here. I have the following program (C++11):

#include  
#inc         


        
3条回答
  •  醉酒成梦
    2021-01-02 07:03

    boost::split always returns n + 1 tokens where n is number of separators in the input string. So don't be surprised when it returns 1 token when you pass it an empty string.

    The rationale behind it is quite simple. Imagine that you are parsing a CSV file. You need to get the exactly same number of elements regardless whether the last token is empty or not.

    It is much easier to remove the empty tokens than to guess whether they were supposed to be in the result or not. Credit

    This behaviour is similar to python

    >>> len("".split(','))
    1
    

提交回复
热议问题