例如:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
int num = 10;
string str;
stringstream toString;
toString << num;
toString >> str;
cout << str << "\n"; //10
int num2 = 20;
string str2;
toString << num2;
toString >> str2;
cout << str2 << "\n"; //str2 is empty
return 0;
}
我知道我必须像这样清除它:
toString.str("");
toString.clear();
但是为什么在使用operator >>>
后没有自动清除呢?
最佳答案
如果我执行 toString >> a >> b >> c
并且第一个失败,我不希望清除标志以便最终状态看起来已经成功。
关于c++ - 为什么我需要在重用之前清除 stringstream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27906906/