简体   繁体   English

c++:“while (cin >> variable, variable)”究竟是做什么的?

[英]c++: What does 'while (cin >> variable, variable)' exactly do?

So here's that piece of code, very basic but i just can't find any similar questions that have two variables in the while loop with cin in it.所以这是那段代码,非常基本,但我找不到任何类似的问题,它们在 while 循环中有两个变量,其中包含 cin。

void Perm(int start, int end, int a[]) {
  //some recursion code
}
int main() {
   int i, n, a[10];
   while (cin >> n, n) {
      for (i = 0; i < n; i++)
      {
         a[i] = i + 1;
      }
      Perm(0, n, a);
   }
   return 0;
}

Can't figure out what does while (cin >> n, n) part do, and when will it stops.无法弄清楚 while (cin >> n, n) 部分做了什么,什么时候停止。 It looks like when I run the code, the input is just required once..看起来当我运行代码时,只需要输入一次..

The comma operator performs the expression to the left of the comma and discards the result then performs the expression on the right and offers up the result for testing.逗号运算符执行逗号左侧的表达式并丢弃结果,然后执行右侧的表达式并提供结果以供测试。 So, in English所以,用英语

while (cin >> n, n) 

says, read from Standard In into integer n and discard whether or not the read was successful, then test the value of n .说,从 Standard In 读入 integer n并丢弃读取是否成功,然后测试n的值。 If n is not zero, enter the loop.如果n不为零,则进入循环。

With a modern compiler the point is sort of moot, but I believe it would read better as对于现代编译器,这一点有点没有实际意义,但我相信它会读起来更好

while (cin >> n && n)

or in English, if we successfully read from standard in into integer n and n is not zero, enter the loop.或者在英语中,如果我们成功地从标准 in 读取到 integer n并且n不为零,则进入循环。 It conveys the intent better, but it's moot because if the read fails, as of C++11 n will be set to zero and still exit the loop.它更好地传达了意图,但它没有实际意义,因为如果读取失败,截至 C++11 n将被设置为零并仍然退出循环。 Before C++11 you probably got an infinite loop testing the last good, non-zero value of n over and over forever.在 C++11 之前,您可能会遇到一个无限循环,永远不断地测试n的最后一个良好的非零值。

As explored in the comments below the point seems to be not so moot.正如下面评论中所探讨的那样,这一点似乎没有实际意义。 It appears that under some conditions, no data given in this case , the value is not being zeroed.似乎在某些情况下,在这种情况下没有给出数据,该值没有被归零。 More Standard-reading required here, but it looks as though这里需要更多标准阅读,但看起来好像

while (cin >> n && n)

or similar is required to catch all of the cases .或类似的需要捕捉所有的情况

In the condition of the while statement在 while 语句的条件下

while (cin >> n, n) {

there us used the comma operator.我们在那里使用了逗号运算符。

That is the expression consists from two subexpressions, cin >> n and n , that are executed sequentially.也就是说,表达式由两个子表达式cin >> nn组成,它们按顺序执行。 The result of the condition is the value of the second subexpression that is implicitly converted to the type bool.条件的结果是隐式转换为 bool 类型的第二个子表达式的值。

From the C++ 17 Standard (8.19 Comma operator)来自 C++ 17 标准(8.19 逗号运算符)

  1. ... A pair of expressions separated by a comma is evaluated left-to-right; ... 一对由逗号分隔的表达式从左到右求值; the left expression is a discarded-value expression (Clause 8).左边的表达式是一个丢弃值表达式(第 8 条)。 Every value computation and side effect associated with the left expression is sequenced before every value computation and side effect associated with the right expression.与左表达式关联的每个值计算和副作用在与右表达式关联的每个值计算和副作用之前排序。 The type and value of the result are the type and value of the right operand;...结果的类型和值是右操作数的类型和值;...

The while loop can be equivalently rewritten like while 循环可以等效地重写为

while (cin >> n, n != 0 ) {

It would be more safer to write the condition of the while statement like写 while 语句的条件会更安全

while (cin >> n, n != 0 && n <= 10 ) {

Instead of the comma operator it would be better to use the logical AND operator like for example代替逗号运算符,最好使用逻辑 AND 运算符,例如

while (cin >> n && n != 0 && n <= 10 ) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM