Alexandre
Alexandre

Reputation: 817

Random behavior of boost tokenizer

I have a problem with boost tokenizer, here is my code:

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

using namespace std;

static vector<std::string> tokenize(const std::string& input, const char delim) {
    std::cout << "Tokenize: " << input << std::endl;
    vector<std::string> vector;
    typedef boost::char_separator<char> TokenizerSeparator;
    typedef boost::tokenizer<TokenizerSeparator> Tokenizer;
    TokenizerSeparator separator(&delim);
    Tokenizer tokenizer(input, separator);
    Tokenizer::iterator iterator;

    for(iterator=tokenizer.begin(); iterator!=tokenizer.end();++iterator){
        std::cout << "elem found: " + *iterator << std::endl;
        vector.push_back(*iterator);
    }
    return vector;
}

int main(int argc, const char * argv[])
{
    string input = "somedata,somedata,somedata-somedata;more data;more data";
    vector<string> list = tokenize(input, ';');

    return 0;
}

This code does not behave consistently all the time. Some times it works, some times not when run multiple times. When it doesn't work here is one output I get:

Tokenize: somedata,somedata,somedata-somedata;more data;more data
elem found: some
elem found: ata,some
elem found: ata,some
elem found: ata-some
elem found: ata
elem found: more 
elem found: ata
elem found: more 
elem found: ata

What am I doing wrong ?

Thanks.

Upvotes: 0

Views: 260

Answers (2)

Alexandre
Alexandre

Reputation: 817

Thanks to @DavidSchwartz for the answer (see comments above).

char_separator needs a valid C string in its constructor.

Upvotes: 0

David Schwartz
David Schwartz

Reputation: 182753

TokenizerSeparator separator(&delim);

You are tokenizing based on the address the character was stored at rather than the value of the character.

Upvotes: 3

Related Questions