How to split a line and save separators using boost :: split?

I have a line like this:

std::string input("I #am going to# learn how #to use #boost# library#");

I'm doing it:

std::vector<std::string> splitVector;
boost::split(splitVector, input, boost::is_any_of("#"));

And got this: (splitVector)

splitVector:
        "I "
        "am going to"
        " learn how " 
        "to use "
        "boos"
        " library"
        "" // **That odd, why do I have an empty string here ?**

But you need something like this:

splitVector:
    "I "
    "#am going to"
    "# learn how "
    "#to use "
    "#boost"
    "# library"
    "#"

How to do it? Or maybe there is another way to do this in the accelerator library? And why am I getting an empty string in splitVector?

+3
source share
1 answer

You cannot use boost::split, because an internal implementation using split_iteratorof boost/algorithm/string/find_iterator.hppswallows tokens.

However, you can go through with boost::tokenizer, as it has the ability to save the delimiters:

, , , . drop_delims , keep_delims .
http://www.boost.org/doc/libs/1_55_0/libs/tokenizer/char_separator.htm

live:

#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]
[#] */
+5

All Articles