简体   繁体   English

我的for循环执行的次数比预期的多

[英]My for loop is executing more times than is expected

I have the following for loop executing within my program and I can't see how it's design correlates with the output I'm receiving. 我有以下for循环我的计划之内执行,我看不出它是如何设计,我收到的输出相关。

cout << no_of_lines << endl;
for (int count = 0; count < no_of_lines + 1; count ++)
{
    getline(Device, line, '=');
    cout << line << endl;
}

This is the output: 这是输出:

3
DeviceName
GPU
Manufacturer
Intel
GraphicalRam
128MB

And this is the file DeviceList 这是文件DeviceList

DeviceName=GPU
Manufacturer=Intel 
GraphicalRam=128MB

In the loop, no_of_lines refers to the number of lines in the file, in this case 3. I have provided this output as verification that the loop is only executing once per line. 在循环中, no_of_lines指的是文件中的行数,在本例中为3.我提供此输出作为验证,即循环每行只执行一次。 Can anyone tell me why this loop is executing more times than is expected? 谁能告诉我为什么这个循环执行的次数比预期的多? I'm guessing it's because of my inclusion of = as the deliminator, and that the loop is somehow executing an additional time before incrementing, but then why does it stop on the deliminator on the last line, requiring me to add 1 to the loop limit? 我猜这是因为我包含了=作为deliminator,并且循环在某种程度上在递增之前执行了额外的时间,但是为什么它会在最后一行的deliminator上停止,要求我在循环中添加1限制?

This http://www.cplusplus.com/reference/string/getline/ is your friend here. 这个http://www.cplusplus.com/reference/string/getline/是你的朋友。 The documentation says that when delem character is passed to getline, it reads up to delem char is found or end of file is reached. 文档说当将删除字符传递给getline时,它会读取Delem char或找到文件结尾。 '\\n' is not treated differently. '\\ n'没有区别对待。 Once it find delem char it discards it and fills line whatever it read until that point. 一旦发现DELEM烧焦它丢弃并填充线不管它一直读到了这一点。 Next time you call getline, it continues to read from where it left. 下次你打电话给getline时,它会继续从它离开的地方读取。 so in this case, each call reads as follows. 所以在这种情况下,每个调用如下所示。

DeviceName
GPU\nManufacturer
Intel\nGraphicalRam
128MB\n

'\\n' in the above string is basically newline character. 上面字符串中的'\\ n'基本上是换行符。 it's not really backslash followed by n (2 characters). 它不是真正的反斜杠,后跟n(2个字符)。 its just single newline character '\\n'. 它只是一个换行符'\\ n'。 Just for understanding purposes I used it that way. 出于理解目的,我就这样使用它。

For clarity, here is the complete code(compiled and tested). 为清楚起见,这里是完整的代码(编译和测试)。

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int no_of_lines = 3;
    string line;
    cout << no_of_lines << endl;
    for (int count = 0; count < no_of_lines + 1; count++)
    {
        getline(cin, line, '=');
        cout << line << endl ;
    }
    return 0;
}

By now I hope its clear to you why you need to call getline 4 times. 到现在为止,我希望你明白为什么你需要4次拨打getline。

your loop executes no_of_lines+1 times. 你的循环执行no_of_lines+1次。

I assume you want the delimiter to be '='. 我假设您希望分隔符为'='。 However, when the delimiter is '=', then \\n is not a delimiter. 但是,当分隔符为'='时, \\n不是分隔符。 Hence line will contain \\n (say, on the second getline ). 因此, line包含\\n (例如,在第二个getline )。 And so the number of lines displayed is not equal to the number of times the loop executes. 因此显示的行数不等于循环执行的次数。

When you use = as line delimiter, \\n is not used as line delimiter. 使用=作行分隔符时, \\n 不用作行分隔符。

So you get strings containing newline characters. 所以你得到包含换行符的字符串。

When you output such a string, it's presented as two or more lines (due to the newlines it contains). 当您输出这样的字符串时,它会显示为两行或更多行(由于它包含的换行符)。

Cheers & hth., 干杯&hth。,

你想要它计数三次,但你的循环计数0,1,2,3,4,即5次迭代。

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

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