if (cin gt;gt; x) - Why can you use that condition?(if (cin x) - 为什么你可以使用那个条件?)
问题描述
整个夏天我一直在使用Accelerated C++"来学习 C++,但有一个我似乎不太理解的概念.
I have been using "Accelerated C++" to learn C++ over the summer, and there's a concept which I don't seem to understand properly.
为什么
int x;
if (cin >> x){}
相当于
cin >> x;
if (cin){}
通过查看代码,在我看来,我们正在使用 cin 作为变量.但是,我认为这是一个函数.当 x 具有我们在键盘中输入的任何值时,为什么我们可以这样使用 cin?
By looking at the code, it seems to me that we're using cin as a variable. But, I thought it was a function. Why can we use cin in this way when it is x that has whatever value we input into our keyboard?
推荐答案
cin 是 istream 表示标准输入流.它对应于cstdio 流stdin.操作符 >>> 重载流返回对同一流的引用.流本身可以通过转换运算符在布尔条件中评估为真或假.
cin is an object of class istream that represents the standard input stream. It corresponds to the cstdio stream stdin. The operator >>overload for streams return a reference to the same stream. The stream itself can be evaluated in a boolean condition to true or false through a conversion operator.
cin 提供格式化的流提取.操作cin>>x;
cin provides formatted stream extraction. The operation
cin >> x;
如果一个非数字值是一个整数,那么x"是一个整数将会失败进入.所以:
where "x" is an int will fail if a non-numeric value is entered. So:
if(cin>>x)
如果您输入的是字母而不是数字,将返回 false.
will return false if you enter a letter rather than a digit.
这个关于使用 C++ I/O 的技巧和窍门 的网站也会对您有所帮助.
This website on tips and tricks using C++ I/O will help you too.
这篇关于if (cin >> x) - 为什么你可以使用那个条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:if (cin >> x) - 为什么你可以使用那个条件?
基础教程推荐
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
