Duplicate:
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?
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?
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
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. :-)
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.