什么是cin在里面的论证if? [英] What is cin doing inside argument of if?

查看:139
本文介绍了什么是cin在里面的论证if?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

std :: ostringstream如何转换为bool?





#include<iostream.h>
 void main() {
   if(cin>>2) {    // what is cin>>2 doing inside arg of if
      cout<<"if works";
   } else {
       cout<<"else works";
     }
 }

代码。总是如果语句工作 为什么?如何?

推荐答案

cin> 2 无效。 cin>>

cin >> 2 is invalid. cin >> integervar is valid, assuming that's what you mean?

标准库类 ios 是有效的, basic_istream (因此 cin )继承自重载 operator void * (和 operator!),以允许您进行此类测试。

The standard library class ios which basic_istream (and thus cin) inherits from overloads operator void * (and operator!) to allow you to do such tests.

如果 failbit code> badbit 被设置 - 也就是最后一次提取失败。

operator void * returns 0 if failbit or badbit is set - aka the last extraction failed.

这是提取和检查是否成功提取的标准方式。

This is the "standard" way of combining extraction and checking if the extraction succeeded.

这篇关于什么是cin在里面的论证if?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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