简体   繁体   English

C ++:我不明白while循环的工作原理

[英]C++: I don't understand how this while loop works

I was looking at some questions on here that shows you how to read from a txt file, and then assign anything in it to separate variables. 我正在看这里的一些问题,这些问题向您展示了如何从txt文件读取内容,然后将其中的任何内容分配给单独的变量。 The answer used a while loop that looked like this... 答案使用了一个while循环,看起来像这样……

ifstream file(“file.txt”);

int var1;
int var2;
int var3;

while (file >> var1 >> var2 >> var3)
{
    /* do something with name, var1 etc. */
    cout << var1 << var2 << var3 << “\n”;
}

My question is, what is happening in that while loop that allows it assign those variables the right value? 我的问题是,while循环中发生了什么,它允许它为那些变量分配正确的值? I know that it works, because I used it, but I just don't understand how it works. 我知道它有效,因为我使用过它,但是我只是不明白它是如何工作的。

Here's the link I was looking at, just in case anybody wanted to also look at it. 这是我正在查看的链接,以防万一有人想要也查看它。 C++: Read from text file and separate into variable C ++:从文本文件读取并分成变量

Thanks! 谢谢!

Basically, the "expression" (a) file >> var1 >> var2 >> var3 will attempt to read three values from the file stream and place them into the specified variables. 基本上,“表达式” (a) file >> var1 >> var2 >> var3将尝试从file流中读取三个值并将它们放入指定的变量中。 It's basically no different than cin >> myVar on it's own (except that it's chanining the input to multiple variables). 基本上,它本身与cin >> myVar没什么不同(除了它是将输入更改为多个变量)。

The value of that entire expression will be true if and only if all three values are read successfully. 当且仅当成功读取所有三个值时,整个表达式的才为true。

In that case, the loop body will execute, the three values will be printed out, and the loop will go back to the top and try to get the next three values. 在这种情况下,将执行循环主体,将打印出三个值,然后循环将返回顶部并尝试获取下三个值。

At the point where the expression evaluates as false (ie, three values are not read), the loop will stop. 在表达式的计算结果为假的点(即未读取三个值),循环将停止。


(a) The actual pathway from someStream >> someVariable to a Boolean value involves a few steps within the stream class (the return value from operator>> being converted to a Boolean with operator bool ) but the above explanation should be good enough for most purposes. (a)someStream >> someVariable到Boolean值的实际路径涉及流类中的几个步骤(将operator>>的返回值转换为带有operator bool的Boolean),但是以上解释对于大多数人来说应该足够好目的。

ifstream overloads operator>> . ifstream重载operator >> In your particular case it is: 在您的特定情况下,它是:

basic_istream& operator>>( int& value );

as you see it returns reference to itself so you can rewrite: 如您所见,它返回对自身的引用,因此您可以重写:

 while (file >> var1 >> var2 >> var3)

as a function calls: 作为函数调用:

while (file.operator>>(var1).operator>>(var2).operator>>(var3))

additionally ifstream explicitly converts to bool which gives true if stream is ok, otherwise false. 另外,ifstream明确转换为bool,如果stream正常,则为true,否则为false。 This conversion is done using operator bool , which is marked as explicit (C++11 and up). 使用操作符bool进行此转换,该操作符被标记为显式的 (C ++ 11及更高版本)。 This specifier applied to conversion operator allows to use ifstream instance in while loop or if statements, but it will not allow you to for example assign it to bool variable: 应用于转换运算符的该说明符允许在while循环或if语句中使用ifstream实例,但不允许您例如将其分配给bool变量:

bool b = file; // results in error

The compiler will translate: file >> var1 >> var2 >> var3 as: file.operator>>(var1).operator>>(var2).operator(var3) 编译器会将file >> var1 >> var2 >> var3为: file.operator>>(var1).operator>>(var2).operator(var3)

The return of basic_istream::operator>>(int&) is basic_istream& hence the extraction operator can be called repeatedly on it's return because it returns a reference to itself. basic_istream::operator>>(int&)的返回值是basic_istream&因此提取操作符可以在返回时重复调用,因为它返回了对自身的引用。

When you evaluate this as in: while(file >> var1 >> var2 >> var3) the compiler is really doing: while(file.operator>>(var1).operator>>(var2).operator(var3).operator bool()) And we can see that basic_stream provides operator bool() So that will be what is evaluated as the condition for the while loop. 当您将其评估为: while(file >> var1 >> var2 >> var3) ,编译器实际上正在执行: while(file.operator>>(var1).operator>>(var2).operator(var3).operator bool())我们可以看到basic_stream提供了operator bool() ,这将作为while循环的条件进行评估。 And operator bool() : operator bool()

Returns true if the stream has no errors and is ready for I/O operations. 如果流没有错误并且可以进行I / O操作,则返回true Specifically, returns !fail() . 具体来说,返回!fail()

So the human readable interpretation of this line is: "Insert var1 , var2 , and var3 into file . If the insertions didn't fail continue the loop." 因此,这一行的可读解释是:“将var1var2var3插入file 。如果插入没有失败,则继续循环。”

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

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