Timeline for How do I tokenize a string in C++?
Current License: CC BY-SA 2.5
11 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jul 2, 2020 at 23:32 | comment | added | Some Guy |
@fuzzyTew I am not an advocate of using strtok in C++ code. However, when I have needed to convert legacy code using it to avoid naked pointers (and their non-RAII lack of guarantees that what they point to will be deallocated if an exception gets thrown), I have used "vector<char> foo(str.begin(), str.end()+1); char *p = strtok(foo.data(), " "); " That way, the vector owns a writable copy of the string's data and will release it if an exception is thrown. (The +1 is to ensure that the string's null terminator gets copied into the vector.)
|
|
Sep 25, 2019 at 20:26 | comment | added | Gabriel Staples |
strtok_r is thread-safe and better to use when available. Just try it to see if your compiler supports it. It probably does.
|
|
Nov 4, 2016 at 8:48 | comment | added | Vijay Kumar Kanta | This is a nice solution and good in C projects. However it's not compatible with C++ string type literals or objects. Looks like C++ requires a class for everything. >_< | |
Sep 18, 2013 at 18:28 | comment | added | The Chaz 2.0 | @tloach: under what circumstances would a string not contain a null character? I only know that, sometimes, '\0' is appended to strings... (Noob question, I know!) | |
Aug 3, 2013 at 14:43 | comment | added | fuzzyTew | strtok requires a pointer to a non-const null-terminated char array, which is not a common creature to find in c++ code ... what's your favourite way to convert to this from a std::string? | |
Nov 29, 2010 at 13:23 | comment | added | tloach | @ahmed: thread safe means more than just being able to run the function twice in different threads. In this case if the thread is modified while strtok is running it's possible to have the string be valid during the entire run of strtok, but strtok will still mess up because the string changed, it's now already past the null character, and it's going to keep reading memory until it either gets a security violation or finds a null character. This is a problem with the original C string functions, if you don't specify a length somewhere you run into problems. | |
Nov 28, 2010 at 15:03 | comment | added | Ahmed | you can check this nibuthomas.wordpress.com/2008/06/25/… | |
Nov 28, 2010 at 15:03 | comment | added | Ahmed | @tloach: in MS C++ compiler strtok is thread safe as the internal static variable is created on the TLS (thread local storage) (actually it is compiler depended) | |
Oct 6, 2010 at 9:14 | comment | added | Prof. Falken | There is strtok_r, but this was a C++ question. | |
May 10, 2010 at 13:18 | comment | added | tloach | Not that I dislike C, however strtok is not thread-safe, and you need to be certain that the string you send it contains a null character to avoid a possible buffer overflow. | |
Sep 10, 2008 at 13:37 | history | answered | Mark | CC BY-SA 2.5 |