C++ 为什么strtok不能与stringstream一起工作?

C++ 为什么strtok不能与stringstream一起工作?,c++,split,stringstream,strtok,C++,Split,Stringstream,Strtok,考虑以下参数: char words[8] = "one two"; string word1; string word2; stringstream ss; 此代码的输出: ss << strtok(words, " "); ss >> word1; ss << strtok(NULL, " "); ss >> word2; cout << "Words: " << word1 << " " <<

考虑以下参数:

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;
而对于此代码

ss << strtok(words, " ");
ss >> word1;
char* temp = strtok(NULL, " ");
word2 = temp;
cout << "Words: " << word1 << " " << word2 << endl;

为什么
stringstream
可以处理
strtok
的第一个返回值,但不能处理第二个返回值?

您应该插入语句

ss.clear();
以清除流的eof状态。比如说

    char words[8] = "one two";
    std::string word1;
    std::string word2;
    std::stringstream ss;
    ss << std::strtok(words, " ");
    ss >> word1;
    ss.clear();
    ss << std::strtok(NULL, " ");
    ss >> word2;
    std::cout << "Words: " << word1 << " " << word2 << std::endl;
charwords[8]=“一二”;
std::stringword1;
std::stringword2;
std::stringstream-ss;
ss>word1;
ss.clear();
ss>word2;

std::cout您应该插入语句

ss.clear();
以清除流的eof状态。比如说

    char words[8] = "one two";
    std::string word1;
    std::string word2;
    std::stringstream ss;
    ss << std::strtok(words, " ");
    ss >> word1;
    ss.clear();
    ss << std::strtok(NULL, " ");
    ss >> word2;
    std::cout << "Words: " << word1 << " " << word2 << std::endl;
charwords[8]=“一二”;
std::stringword1;
std::stringword2;
std::stringstream-ss;
ss>word1;
ss.clear();
ss>word2;

我猜不出来。输入第一个单词后,
ss
的ostream部分点击EOF并设置
EOF
标志。除非您手动清除该标志,否则无法进一步输入。它可以处理所有这些标志,但您没有正确使用它。您试图使用
stringstream
作为一个容器,但它实际上是一个数据流。只是一个猜测。输入第一个单词后,
ss
的ostream部分点击EOF并设置
EOF
标志。除非您手动清除该标志,否则无法进一步输入。它可以处理所有这些标志,但您没有正确使用它。您试图使用
stringstream
作为容器,但它是一个数据流。