Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.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++_Arrays_Stringstream - Fatal编程技术网

C++ 字符串流到数组

C++ 字符串流到数组,c++,arrays,stringstream,C++,Arrays,Stringstream,你能告诉我,为什么这是错误的吗 我有 mytype test[2]; stringsstream result; int value; for (int i=0; i<2; i++) { result.str(""); (some calculating); result<< value; result>> test[i]; } mytype测试[2]; 串流结果; int值; 对于(int i=0;i测试[i]; } 当我观察测试数组

你能告诉我,为什么这是错误的吗

我有

mytype test[2];
stringsstream result;
int value;

for (int i=0; i<2; i++) {
   result.str("");
   (some calculating);
   result<< value;
   result>> test[i];
}
mytype测试[2];
串流结果;
int值;
对于(int i=0;i测试[i];
}
当我观察测试数组时-只有第一个-测试[0]-具有正确的值-每隔一个测试[1..x]具有值0 为什么它是错误的并且不起作用?在第一个运行周期中,stringstream将正确的值设置为array,但后来只有0

谢谢

请尝试
result.clear()
在使用
result.str(“”
)刷新您的stringstream之前,将其设置为在输出缓冲区后再次接受输入的状态

#include <sstream>
using namespace std;

int main(){
    int test[2];
    stringstream result;
    int value;

    for (int i=0; i<2; i++) {
        result.clear();
        result.str("");
        value = i;
        result<< value;
        result>> test[i];
    }

    return 0;
}
#包括
使用名称空间std;
int main(){
int检验[2];
流结果;
int值;
对于(int i=0;i测试[i];
}
返回0;
}
不清除时,我得到
test[0]==0
test[1]=-832551553/*一些随机数*/
。清除时,我得到
test[0]==0
test[1]==1
的预期输出