I'm making this question because I moved my tokenizer from strtok_r to an equivalent version in C++. I have to use strtok_r in place of strtok, because I have 2 nested tokenizations to perform most of the time.
The strtok_r algorithm is something like this:
char *end_token, *token, *word ;
// fill 'word'
token = strtok_r (word, " ", &end_token) ;
while (token != NULL) {
// do something
token = strtok_r (NULL, " ", &end_token) ;
}
And the C++ version is like this (taken from another post here):
string mystring, token ;
size_t next_token ;
// fill 'mystring'
while (token != mystring) {
next_token = mystring.find_first_of (" ") ;
token = mystring.substr (0, next_token) ;
mystring = mystring.substr (next_token + 1) ;
// do something
}
Now the question: why the C++ version is so heavy respect to the C version? For long strings I have to wait about 10 seconds with the C++ version, while the C version is instantaneous with the same strings. So, it seems like the C++ version has higher complexity... What do you think about?