-1

在文本文件中,前 8 行是文本,我不需要,所以在我将文本文件读入之前vector...我想做一个计数,所以它基本上从第 9 行开始读取文件;

if (myfile.is_open())
{
    while ( getline(myfile, line) )
    {
        if count > 8;
        istringstream buffer(line);
        int x, y; 
        if (!(buffer >> x >> y));

        Station objName = {x, y};
        data_station.push_back(objName);
        count == count +1;
    }
}

这是我要做的,但我似乎无法理清计数。

4

3 回答 3

6

这不是任务

count == count +1;

但是是一个相等检查意味着count' 的值永远不会改变。改成:

count++;

或者:

// See comment from rhalbersma.
++count;

后面还有一个分号(更不用说缺少括号):

if count > 8;

改成:

if (count > 8)
{
    istringstream buffer(line);
    int x, y; 
    if (buffer >> x >> y) // Correction here also.
    {
        Station objName = {x, y};
        data_station.push_back(objName);
    }
}
于 2013-04-04T09:31:23.057 回答
1

首先,您需要使用赋值而不是测试相等性:

count = count + 1;
//    ^ here

但是,这可以更简洁地写成:

count++;

另外,请注意if语句的语法需要在条件周围加上括号。要将许多语句组合在一起作为 的一部分if,请引入一个带有{and的块}

if (condition) {
  block of statements
}

所以你的代码应该是这样的:

if (count > 8) {
  istringstream buffer(line);
  int x, y; 
  if (!(buffer >> x >> y)) {
    Station objName = {x, y};
    data_station.push_back(objName);
  }
}

看起来你的内在if条件也倒退了。您希望在提取成功时执行块:

if (buffer >> x >> y)
于 2013-04-04T09:33:31.130 回答
0

改变这个count == count +1;count = count +1;

更好的是count++;

于 2013-04-04T09:32:04.747 回答