保存在我的文件中的数据是(在此测试的开头和结尾都添加了空格):
1 2 3
使用以下带有或不带有“std::ws”的代码加载数据不会造成任何差异。所以我对“std::ws”的作用感到困惑,因为我看到了使用它的代码。有人可以解释一下吗?谢谢!
void main ()
{
ifstream inf;
inf.open ("test.txt");
double x=0, y=0, z=0;
string line;
getline(inf, line);
istringstream iss(line);
//Using "std::ws" here does NOT cause any difference
if (!(iss >> std::ws >> x >> y >> z >> std::ws))
{
cout << "Format error in the line" << endl;
}
else
{
cout << x << y << z << endl;
}
iss.str(std::string ());
iss.clear();
cin.get();
}
Run Code Online (Sandbox Code Playgroud)