tags:

views:

1069

answers:

3

Duplicate:

How do I tokenize a string in C++?

I have a character array in C++.

arr="abc def ghi"

I want to get the strings "abc" "def" "ghi" out of the string. Are there any built in functions to do this?

+3  A: 

Sure you can use the stringstream class and the extraction operators:

stringstream str("abc def ghi");
string a, b, c;
str >> a >> b >> c; // ta da
1800 INFORMATION
An explanation on why it works and what is happening would be nice!
Martin York
Why it works is because of magic :)
1800 INFORMATION
+3  A: 

Building on 1800's excellent answer:

#include <iterator>
#include <sstream>
#include <string>
#include <vector>

using std::istream_iterator;
using std::istringstream;
using std::string;
using std::vector;

vector<string>
cheap_tokenise(string const& input)
{
    istringstream str(input);
    istream_iterator<string> cur(str), end;
    return vector<string>(cur, end);
}

Geekery ahead: I would have liked to use pass-by-value of the string, due to this article: Move constructors. But that technique is currently moot, since basic_istringstream's constructor takes the string by const reference, and (in basic_stringbuf's constructor) copies it. I dream of better days ahead, when the standard library (and common compilers!) supports the techniques mentioned in that article. :-)

Chris Jester-Young
Thank's for the complement but I don't know if mine was really excellent. It was pretty much off the cuff - also it's hard to come up with anything too memorable for a question like this
1800 INFORMATION
Well, my original response would have been to use boost::tokenizer, but yours is a much more "lightweight" solution, so I used that as the basis of my answer.
Chris Jester-Young
In this case pass-by-value it's not worth since you are not doing a copy: you are building an istreamstream from.
Nicola Bonelli
I agree, hence my comment about "dreaming of better days". :-) But perhaps you're right, I should edit my post.
Chris Jester-Young
Very interesting article by the way
1800 INFORMATION
A: 

This task is more commonly called string tokenizing. There was a thread: http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c

To conclude. There are a couple of way to that. What is the best depends on what api you want to\need to use.

  • c-style: strtok() function
  • stl: std::stringstream
  • boost: boost::tokenizer, boost::split
begray
strtok is yuck, don't go there. The other options are just fine. :-)
Chris Jester-Young
I quite like strtok()!
Matthew Schinckel
The trouble with strtok() is that it mutilates the string.
Martin York
Additionally, ou can also use getline() with a delimiter to do simple tokenization.
Martin York
@Matthew: strtok is not reentrant, and like Martin says, it mutates the string.
Chris Jester-Young