while(!cin.eof())vs. while(cin>> n)

时间:2015-02-25 03:49:14

标签: c++ while-loop

哪个最适合在C ++中使用?这些用法之间的主要区别之一如下:

int n;

// get the first item, if it exists
cin >> n;
while (!cin.eof()) {
   // do what ever the loop should do (edit the input)

   // get the next item
   cin >> n;
}

while (cin >> n) {
// we can just use the data as it comes in
}

第二个实现无疑是更短的,是我的第一选择,但我之前对简约代码的看法是错误的。

0 个答案:

没有答案