1

Possible Duplicate:
How do I tokenize a string in C++?

strtok function is not thread safe. Microsoft has Windows-specific strtok_s and CString::Tokenize safe functions. Is there cross-platform CRT/C++ library way to do this without manual coding?

Community
  • 1
  • 1
Alex F
  • 42,307
  • 41
  • 144
  • 212

3 Answers3

5

boost::split. http://www.boost.org/doc/libs/1_51_0/doc/html/string_algo/reference.html#header.boost.algorithm.string.split_hpp

example of usage

#include <vector>
#include <string>
#include <boost/algorithm/string.hpp>

int main()
{
   const std::string s = "hello and what";
   std::vector<std::string> v;
   boost::split(v, s, [](char c) { return c == ' '; }, boost::token_compress_on);
   for (const auto& str : v)
   {
      std::cout << str << std::endl;
   }
}

http://liveworkspace.org/code/3dfc9ee9c8497741f9976ac41a14a390

Or use boost::tokenizer

ForEveR
  • 55,233
  • 2
  • 119
  • 133
2

The thread safe version of strtok() is strtok_r(). And it is POSIX compliant too.

Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
2

Actually, strtok() is generally thread safe (virtually any runtime supporting a multithreaded OS will have a thread-safe strtok()). strtok() can't be used to tokenize different strings in an 'alternating' fashion, but that's a pretty rare occurrence and is under control of your code.

However, that said, strtok_r() is a common (though not part of the C standard - it's POSIX) variant that lets you maintain control over the context so you can juggle as many strtok_r() contexts at the same time as you like. It's not available with MSVC, but you can find a public domain implementation here: http://snipplr.com/view/16918/

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • Moreover strtok_r (on POSIX side) and strtok_s (on Visual Studio side) have exactly the same prototype and behaviour. With a simple macro that maps strtok_r to strtok_s on Windows targets you should have a portable solution. – greydet Aug 22 '12 at 07:12