【发布时间】:2013-12-28 12:11:07
【问题描述】:
考虑这些参数:
char words[8] = "one two";
string word1;
string word2;
stringstream ss;
这段代码的输出:
ss << strtok(words, " ");
ss >> word1;
ss << strtok(NULL, " ");
ss >> word2;
cout << "Words: " << word1 << " " << word2 << endl;
是:
Words: one
这段代码
ss << strtok(words, " ");
ss >> word1;
char* temp = strtok(NULL, " ");
word2 = temp;
cout << "Words: " << word1 << " " << word2 << endl;
输出是:
Words: one two
为什么stringstream 可以处理strtok 的第一个返回值而不能处理第二个?
【问题讨论】:
-
只是猜测。输入第一个单词后,
ss的 ostream 部分命中 EOF 并设置eof标志。除非您手动清除该标志,否则无法进一步输入。 -
它可以处理所有这些,但是您没有正确使用它。您正在尝试将
stringstream用作容器,但它是一个数据流。
标签: c++ split stringstream strtok