如何拆分字符串并使用boost :: split保留分隔符?

Rom*_*omz 4 c++ boost

我有一个像这样的字符串:

std::string input("I #am going to# learn how #to use #boost# library#");
Run Code Online (Sandbox Code Playgroud)

我这样做:

std::vector<std::string> splitVector;
boost::split(splitVector, input, boost::is_any_of("#"));
Run Code Online (Sandbox Code Playgroud)

得到了这个:(splitVector)

splitVector:
        "I "
        "am going to"
        " learn how " 
        "to use "
        "boos"
        " library"
        "" // **That's odd, why do I have an empty string here ?**
Run Code Online (Sandbox Code Playgroud)

但需要这样的东西:

splitVector:
    "I "
    "#am going to"
    "# learn how "
    "#to use "
    "#boost"
    "# library"
    "#"
Run Code Online (Sandbox Code Playgroud)

怎么做 ?或者也许在boost库中有另一种方法可以做到这一点?为什么我得到一个空字符串splitVector

moc*_*ace 6

您无法使用,boost::split因为使用split_iteratorfrom 的内部实现boost/algorithm/string/find_iterator.hpp吞下了令牌.

但是你可以使用boost::tokenizer它,因为它有一个保留分隔符的选项:

每当在输入序列中看到分隔符时,当前令牌就完成了,并且新的令牌开始.dropped_delims中的分隔符不会在输出中显示为令牌,而keep_delims中的分隔符确实显示为令牌.
http://www.boost.org/doc/libs/1_55_0/libs/tokenizer/char_separator.htm

见下一个直播:

#include <iostream>
#include <string>
#include <boost/tokenizer.hpp>

int main() {
    // added consecutive tokens for illustration
    std::string text = "I #am going to# learn how ####to use #boost# library#";    
    boost::char_separator<char> sep("", "#"); // specify only the kept separators
    boost::tokenizer<boost::char_separator<char>> tokens(text, sep);
    for (std::string t : tokens) { std::cout << "[" << t << "]" << std::endl; }
}
/* Output:
[I ]
[#]
[am going to]
[#]
[ learn how ]
[#]
[#]
[#]
[#]
[to use ]
[#]
[boost]
[#]
[ library]
[#] */
Run Code Online (Sandbox Code Playgroud)