Re: Split up a string into tokens - split by space
MrAsm wrote:
On Mon, 9 Oct 2006 11:50:13 -0400, "Igor Tandetnik"
<itandetnik@mvps.org> wrote:
string sentence;
istringstream is(sentence);
while (is) {
string word;
is >> word;
cout << word << endl;
}
The error checking here is incorrect, since it doesn't check the input
operation succeeded before outputting the word, hence the code will
print an additional empty line (since word will be the empty string
after a failed operation).
May I ask why if I put the definition of "word" variable out of the
while-block, last token is always printed twice??
i.e.
<CODE>
string sentence = "Hello world";
istringstream is(sentence);
string word;
while (is) {
is >> word;
cout << word << endl;
}
</CODE>
Output:
Hello
world
world
(I compiled the code using VC6 and STLport.)
You've got the same problem here, except that word won't be the empty
string, but whatever it was after the last successful operation, namely
"world".
The code should be this:
<CODE>
string sentence = "Hello world";
istringstream is(sentence);
string word;
while (is >> word) {
cout << word << endl;
}
</CODE>
You should check that the stream is in a good state after each input
operation, otherwise you end up outputting word after a failed
operation, and hence outputting the value it had before the failed
operation.
Tom