整个夏天我一直在使用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) - 为什么你可以使用那个条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
编译器如何处理编译时分支?What do compilers do with compile-time branching?(编译器如何处理编译时分支?)
我可以使用 if (pointer) 而不是 if (pointer != NULL) 吗Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 吗?)
在 C/C++ 中检查空指针Checking for NULL pointer in C/C++(在 C/C++ 中检查空指针)
比较运算符的数学式链接-如“if((5<j<=1))&quMath-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比较运算符的数学式链接-如“if((5<j<=1)))
“if constexpr()"之间的区别与“if()"Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之间的区别与“if())
C++,'if' 表达式中的变量声明C++, variable declaration in #39;if#39; expression(C++,if 表达式中的变量声明)