C++ while(cin) 和 while(cin >> num) 有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19483126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between while(cin) and while(cin >> num)
提问by Ahmed_Mohsen
What the difference between the following two loops and When each one will stopped ?
下面的两个循环和当每个循环停止时有什么区别?
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
int x,y;
while(cin >> x){
// code
}
while(cin){
cin >> y;
//code
}
return 0;
}
回答by templatetypedef
Let's look at these independently:
让我们分别看一下这些:
while(cin >> x) {
// code
}
This loop, intuitively, means "keep reading values from cin
into x
, and as long as a value can be read, continue looping." As soon as a value is read that isn't an int
, or as soon as cin
is closed, the loop terminates. This means the loop will only execute while x
is valid.
这个循环,直观地表示“继续从cin
into 中读取值x
,只要可以读取值,就继续循环”。一旦读取的值不是int
,或者一旦cin
关闭,循环就会终止。这意味着循环只会在x
有效时执行。
On the other hand, consider this loop:
另一方面,考虑这个循环:
while(cin){
cin >> y;
//code
}
The statement while (cin)
means "while all previous operations on cin
have succeeded, continue to loop." Once we enter the loop, we'll try to read a value into y
. This might succeed, or it might fail. However, regardless of which one is the case, the loop will continue to execute. This means that once invalid data is entered or there's no more data to be read, the loop will execute one more time using the old value of y
, so you will have one more iteration of the loop than necessary.
该语句的while (cin)
意思是“当之前的所有操作cin
都成功时,继续循环”。一旦我们进入循环,我们将尝试将一个值读入y
. 这可能会成功,也可能会失败。但是,无论是哪种情况,循环都会继续执行。这意味着一旦输入了无效数据或没有更多数据要读取,循环将使用 的旧值再执行一次y
,因此您将多进行一次循环迭代。
You should definitely prefer the first version of this loop to the second. It never executes an iteration unless there's valid data.
你绝对应该更喜欢这个循环的第一个版本而不是第二个。除非有有效数据,否则它永远不会执行迭代。
Hope this helps!
希望这可以帮助!
回答by Luchian Grigore
The difference is that if cin >> whatever
evaluates to false, your second version still runs the rest of the loop.
不同之处在于,如果cin >> whatever
计算结果为 false,则您的第二个版本仍会运行循环的其余部分。
Let's assume cin >> whatever
fails. What will happen?
让我们假设cin >> whatever
失败。会发生什么?
while(cin >> x){
// code that DOESN'T RUN
}
while(cin){
cin >> y;
//code that DOES RUN, even if the previous read failed
}
回答by paddy
while(cin >> x){
// code
}
This reads integers until it encounters a non-integer, EOF, or other stream error. Whenever you use x
inside the loop, you know it has been read successfully.
这会读取整数,直到遇到非整数、EOF 或其他流错误。每当您x
在循环内部使用时,您就知道它已被成功读取。
while(cin){
cin >> y;
//code
}
This reads integers until it encounters a non-integer, EOF, or other stream error. However, the stream is only checked beforereading the integer. When you use y
in the loop, you can't guarantee that it was successfully read.
这会读取整数,直到遇到非整数、EOF 或其他流错误。但是,仅在读取整数之前检查流。y
在循环中使用时,不能保证读取成功。
回答by Caesar
cin >> x
will store the input value into x.
cin >> x
将输入值存储到 x 中。
As for while(cin)
, std::cin
will return a boolean on whether an error flag is set. Therefore, you will continue in the while loop so long as std::cin
has no error flag set internally. An error flag can be set if it finds an end of file
character, or if it failed to read and store into the value.
至于while(cin)
,std::cin
将返回一个关于是否设置错误标志的布尔值。因此,只要std::cin
内部没有设置错误标志,您将继续在 while 循环中。如果它找到一个end of file
字符,或者它未能读取并存储到值中,则可以设置错误标志。