Possible Duplicate:
Splitting a string in C++
In PHP, the explode()
function will take a string and chop it up into an array separating each element by a specified delimiter.
Is there an equivalent function in C++?
Possible Duplicate:
Splitting a string in C++
In PHP, the explode()
function will take a string and chop it up into an array separating each element by a specified delimiter.
Is there an equivalent function in C++?
Here's a simple example implementation:
Usage:
Note: @Jerry's idea of writing to an output iterator is more idiomatic for C++. In fact, you can provide both; an output-iterator template and a wrapper that produces a vector, for maximum flexibility.
Note 2: If you want to skip empty tokens, add
if (!token.empty())
.The standard library doesn't include a direct equivalent, but it's a fairly easy one to write. Being C++, you don't normally want to write specifically to an array though -- rather, you'd typically want to write the output to an iterator, so it can go to an array, vector, stream, etc. That would give something on this general order: