如何"std :: cin>值"?在while循环中评估? [英] How is "std::cin>>value" evaluated in a while loop?

查看:109
本文介绍了如何"std :: cin>值"?在while循环中评估?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在自学C ++ Primer 5th.这是我不确定的事情. (我在F.A.Q上找不到确切相关的问题.

Currently I'm self-learning C++ Primer 5th. Here comes something I'm not sure. (I couldn't find the exact relevant question on F.A.Q).

考虑以下while循环:

Consider this while loop:

while(std::cin>>value){...}  \\value here was defined as int. 

课本上写着:

该表达式从标准输入中读取下一个数字,并将该数字存储在value中.输入运算符(第1.2节,第8页)返回其左操作数,在这种情况下为std :: cin.因此,此条件将测试std :: cin.当我们使用istream作为条件时,其作用是测试流的状态.如果流有效(即流没有遇到错误),则测试成功.

That expression reads the next number from the standard input and stores that number in value. The input operator (§ 1.2, p. 8) returns its left operand, which in this case is std::cin. This condition, therefore, tests std::cin.When we use an istream as a condition, the effect is to test the state of the stream. If the stream is valid—that is, if the stream hasn’t encountered an error—then the test succeeds.

我的问题是:std::cin首先将输入读入value然后测试std::cin的有效性,还是先测试std::cin然后决定是否读入'值'?我对它何时返回其左操作数"感到非常困惑.

My question is: does std::cin read input into value first then test the validation of std::cin, or test std::cin first then decide whether to read into 'value'? I'm quite confused about when it "returns its left operand".

推荐答案

请记住,您的代码等效于:

Remember that your code is equivalent to:

while (std::cin.operator>>(value)) { }

或者:

while (1) {
    std::cin >> value ;
    if (!std::cin) break ;
}

在测试std::cin之前,代码"总是尝试从std::cin读入value.

The "code" always tries to read from std::cin into value before testing std::cin.

让我们看看报价:

[...]输入运算符(第1.2节,第8页)返回其左操作数,在这种情况下为std :: cin. [...]

[...] The input operator (§ 1.2, p. 8) returns its left operand, which in this case is std::cin. [...]

这仅表示std::cin.operator>>(value)返回std::cin.

因此,此条件将测试std :: cin.当我们使用istream作为条件时,其作用是测试流的状态.如果流有效(即流没有遇到错误),则测试成功.

This condition, therefore, tests std::cin. When we use an istream as a condition, the effect is to test the state of the stream. If the stream is valid—that is, if the stream hasn’t encountered an error—then the test succeeds.

教科书所说的是,在尝试从std::cinvalue读取整数之后,>>运算符返回std::cin.如果std::cin在读取value之后处于良好状态,则测试通过,否则将失败.

What the text book says is that after trying to read an integer from std::cin to value, the >> operator returns std::cin. If std::cin is in a good state after reading value, then the test passes, otherwize it fails.

一些额外的细节:

当您执行std::cin >> value时,您基本上会调用istream::operator>>(int&),并且里面有一个测试 该方法:如果测试通过,则内部状态std::cin的值设置为 ios_base::goodbit ,如果失败,则将内部状态设置为错误标记( eofbit badbit ).

When you do std::cin >> value, you basically call istream::operator>>(int&), and yes there is a test inside that method: If the test passes, then the internal state of std::cin is set to ios_base::goodbit, if it fails, internal state is set to on of the error flag (eofbit, failbit or badbit).

取决于std::cin exception mask ,如果内部测试失败,可能会引发异常..

根据您的报价:

使用istream作为条件时,其作用是测试流的状态.

When we use an istream as a condition, the effect is to test the state of the stream.

这基本上意味着:

if (std::cin) { }

等效为:

if (!std::cin.fail()) { }

std::cin.fail()检查failbitbadbit.这意味着while (std::cin >> value) { }不会测试eofbit标志,并且只会在无法将输入转换为整数值时失败.

And std::cin.fail() check for failbit or badbit. This means that while (std::cin >> value) { } does not test the eofbit flag and will only fail when the input cannot be converted to an integer value.

这篇关于如何"std :: cin>值"?在while循环中评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆