Search code examples
c++c++11boosttokenizeboost-tokenizer

How to tokenize string by delimiters?


I need to tokenize string by delimiters.

For example:

For "One, Two Three,,, Four" I need to get {"One", "Two", "Three", "Four"}.

I am attempting to use this solultion https://stackoverflow.com/a/55680/1034253

std::vector<std::string> strToArray(const std::string &str,
                                    const std::string &delimiters = " ,")
{
    boost::char_separator<char> sep(delimiters.c_str());
    boost::tokenizer<boost::char_separator<char>> tokens(str.c_str(), sep);

    std::vector<std::string> result;
    for (const auto &token: tokens) {
        result.push_back(token);
    }

    return result;
}

But I get the error:

boost-1_57\boost/tokenizer.hpp(62): error C2228: left of '.begin' must have class/struct/union type is 'const char *const'


Solution

  • Change this:

    boost::tokenizer<boost::char_separator<char>> tokens(str.c_str(), sep);
    

    To this:

    boost::tokenizer<boost::char_separator<char>> tokens(str, sep);
    

    Link: http://www.boost.org/doc/libs/1_57_0/libs/tokenizer/tokenizer.htm

    The container type requires a begin() function, and a const char* (which is what c_str()) returns does not meet this requirement.