关于"cin>>"输入成功或失败时的“返回值”(转载)

本文探讨了C++中使用`cin>>`作为条件判断时的工作原理。当`istream`处于有效状态,即仍能继续读取输入时,条件测试成功。遇到文件结束符或无效输入时,`istream`变为无效状态,导致条件失败。通过分析`cin>>`的返回值以及相关头文件,揭示了`cin>>`在while循环中如何转换为布尔值进行判断。
摘要由CSDN通过智能技术生成

今天在看c++primer的时候,读到其中这样一段话:

When we use an istream as a condition, the effect is to test the state of the stream. If the stream is validthat is, if it is still possible to read another input then the test succeeds. An istream becomes invalid when we hit end-of-file or encounter an invalid input, such as reading a value that is not an integer. An istream that is in an invalid state will cause the condition to fail.

其对应代码是:while (std::cin >> value)

                                   ...
开始就觉得这样有点不对劲,但是也不知道是哪里不对劲.仔细一想,原来是觉得这个cin的位置十分诡异...

原来我们最常用的就是直接输入或者直接输出.比如cin>>value;或者cout<<value;现在把这个放到while里面进行判断.

难道cin与cout的返回值是bool型的?好像也说不过去.

GOOGLE之~~

 

分下面几点来说明:

1.c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值