Jump to content
  • Advertisement
Sign in to follow this  
gp343

C++ equivalent to strtok

This topic is 3664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Guys, In pure C, one can use the strtok function and tokenize a set of strings. I was just wondering if in C++ there is an equivalent functions? Thanks, Jr

Share this post


Link to post
Share on other sites
Advertisement
You can construct a stringstream from the string, then use the string extraction operator to pull off individual words (or numbers). You can also use boost::tokenizer for a different interface and a little more control.

Share this post


Link to post
Share on other sites
in general, for string operations in C++, look at the std::string class and other related classes like stringstream

here is the link to cplusplus's page on strings

http://www.cplusplus.com/reference/string/

here is the link to cplusplus's page on the IOstream library,

http://www.cplusplus.com/reference/iostream/

this includes all input/output operations, as well as the stringstream class, which allows you to pretty much write to and read from a string in your memory like you were writing directly to console or to a file (so you get to use stuff like cin and cout directly on a string you control, it's pretty sweet)

probably more information than you wanted, but if you have any C++ string issues, I would browse those documentation pages for functions which do what you want first, cplusplus.com is a really good resource for info on strings and STL container stuff in general.

Share this post


Link to post
Share on other sites
Quote:
Original post by gp343
Hi Guys,

In pure C, one can use the strtok function and tokenize a set of strings. I was just wondering if in C++ there is an equivalent functions?


strtok() still exists in C++, just like the rest of the C standard library. However, there are plenty of better functions to use. Note that strtok() is widely considered one of the worst bits of the C standard library. (There are some UNIX-like systems out there where the documentation says "never use this" in the bugs section.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!