Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;无法重新填充字符串流_C++_Stringstream - Fatal编程技术网

C++ c++;无法重新填充字符串流

C++ c++;无法重新填充字符串流,c++,stringstream,C++,Stringstream,我清空了一条细流,然后又试图再次填满,但没有成功。我不明白为什么。这是我的密码: #include <string> #include <iostream> #include <sstream> using namespace std; int main(int argc, char* argv[] ) { stringstream ss1, ss2; ss1 << "0 1 2 3 40 5 6 76 8 9"; // Stream i

我清空了一条细流,然后又试图再次填满,但没有成功。我不明白为什么。这是我的密码:

#include <string>
#include <iostream>
#include <sstream>

using namespace std;

int main(int argc, char* argv[] ) {

 stringstream ss1, ss2;
 ss1 << "0 1 2 3 40 5 6 76 8 9"; // Stream is filled with one string of chars
 vector <int> V;
 string st;
 int number;


 while(!ss1.eof() ) {
  ss1 >> number;  // Next int found in ss1 is passed to number
  V.push_back(number);
  ss2 << number << " "; // ss2 is filled with number + space in each iteration.  
 }   // Basically here, the content of ss1 has been passed to ss2, leaving ss1 empty.

 ss1 << "helloooo";
 getline(ss1, st);
 cout << st << endl; // <--- Here, st appears to be empty... Why ?

 return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[]){
stringstream ss1、ss2;
ss1>number;//在ss1中找到的下一个int被传递给number
V.推回(编号);

ss2首先,您应该在尝试读取流后,通过将流转换为布尔值来检查读取流是否成功,例如:

while (ss1 >> number) {
    ...
}
在输入后不进行测试往往会导致处理最后一个输入两次。现在,一旦此循环终止,
ss1
处于失败状态,即它设置了
std::ios\u base::failbit
。此时,流将拒绝执行任何其他操作,直到位被清除。您可以使用
clear()
要重置流状态:

ss1.clear();

之后,流应该再次处于良好状态。

因为您点击了eof,流处于错误状态。您必须重置它才能再次使用它。在您的情况下,我会放弃重置,只使用一个新的stringstream对象

哦,在
ss1>>number
之后,您应该在使用
number
之前检查
ss1
的状态。
<代码> EOF()>代码>不返回<代码> true在上次读取失败

之前,尽管您仍然需要<代码>()(代码)>流,在读完之后再写入它,您可以考虑使用<代码> ISTRAMAMIATORATER < /Cult> S来读取文件中的数据:

stringstream ss1("0 1 2 3 40 5 6 76 8 9");

// initialize V from ss1
vector <int> V{std::istream_iterator<int>(ss1), std::istream_iterator<int>()};

// write values from V to ss2
std::copy(V.begin(), v.end(), std::ostream_iterator<int>(ss2));

ss1.clear();
ss1 << "helloooooo";
stringstream ss1(“01 2 3 40 5 6 76 8 9”);
//从ss1初始化V
向量V{std::istream_迭代器(ss1),std::istream_迭代器();
//将值从V写入ss2
std::copy(V.begin()、V.end()、std::ostream_迭代器(ss2));
ss1.clear();

ss1
while(!eof())
确实有问题。使用
while(ss1>>number)
。它也可以按您的方式工作,但为什么eof()会有问题?无论如何,我的ss1 stringstream仍然无法接受另一个字符串:(怎么会出现问题?
eof()的问题)
是指它可能永远无法到达,例如,当您在字符串中添加一个非数字时:流将进入故障模式(即,
std::ios_base::failbit
设置),但除非您克服了这一点,否则它将永远无法到达EOF(即,
std::ios_base::eofbit
将永远无法设置)。更重要的是,您需要在尝试读取后检查您的流,因为流不知道您要尝试什么。正是这样问的。上一次输入确实重复了两次。谢谢,它工作得很好:)