小编H. *_*ang的帖子

std::ws(空格)在读取数据时的作用

保存在我的文件中的数据是(在此测试的开头和结尾都添加了空格):

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)

c++ whitespace stringstream getline istringstream

5
推荐指数
2
解决办法
9264
查看次数

标签 统计

c++ ×1

getline ×1

istringstream ×1

stringstream ×1

whitespace ×1