简体   繁体   English

不同的结果取决于C ++中for循环的循环索引的位置

[英]Different results depending on the location of for loop's looping index in C++

  size_t j = 1; size_t i = 1;

  for (i=1;i<=a;i++){
    for (j=1;j<=b;j++){
      board[i][j] = std::min( std::min(board[i - 1][j] + 1,board[i][j - 1] + 1),
                                          board[i - 1][j - 1] + (A[i - 1] == B[j - 1] ? 0 : 1) );
                                          //cout << board[i][j] << endl;
    }

  }
  cout << board[a][b] << endl;

So I'm just learning C++: if you look at the code above, i and j are defined as first parameters of two for loops. 因此,我只是在学习C ++:如果您看上面的代码,则i和j被定义为两个for循环的第一个参数。 Because I declared i and j prior to this, I believe I can do this as well: 因为我在此之前声明了i和j,所以我相信我也可以这样做:

for (;i<=a;i++
  for (;j<=b;j++)

which in my entire code produces erroneous results. 在我的整个代码中会产生错误的结果。 The first works correctly, however. 但是,第一个正常工作。 Can anyone throw some light on this? 谁能对此有所启发? thanks in advance 提前致谢

And I already did a search on this and am aware of the difference in scope of the index variable. 而且我已经对此进行了搜索,并且意识到索引变量范围的不同。

It's because of scope, basically, in your original code the j variable is reset to one at every iteration of the first loop: the for (j=1;....) . 这是因为范围的原因,基本上,在您的原始代码中, j变量在第一个循环的每次迭代中都会重置为一个: for (j=1;....) However, in the second version of your code, there is no reset. 但是,在第二版代码中,没有重置。

In your first example, i starts at 1 and is incremented until it reaches the value of a. 在您的第一个示例中,i从1开始并递增,直到达到a的值。 j starts at 1 and is incremented until it reaches the value of b. j从1开始并递增,直到达到b的值。

You seem to think the first loop happens, and then the other loop happens. 您似乎认为第一个循环发生,然后另一个循环发生。 That is incorrect. 那是不对的。 The j-loop iterates b-times for every iteration of the i-loop . 对于i循环的每次迭代, j循环都会迭代b次。 Because the first thing that happens on each iteration of the outer loop is j is set back to 1, you have no problems. 因为在外循环的每次迭代中发生的第一件事是将j设置回1,所以没有问题。

In your second example, i starts at 1 and is incremented until it reaches the value of a, just like before. 在您的第二个示例中,i从1开始并递增,直到达到a的值为止,就像以前一样。 j starts at 1 and is incremented until it reaches the value of b, which happens in the first iteration of the outer loop. j从1开始并递增,直到达到b的值为止,这在外循环的第一次迭代中发生。 On the second iteration of the outer loop, i is incremented to 2, but j has not been set back to 1, so it is incremented to whatever b is, and then b+1 on the next iteration of i, etc. 在外循环的第二次迭代中,i递增为2,但j尚未设置回1,因此它将递增为b,然后在i的下一次迭代中递增b + 1,依此类推。

To make them equivalent, you'd need to set j to 1 in the body of the outer loop, just before the inner loop: 为了使它们等效,您需要在内部循环之前将j在外部循环的主体中设置为1:

  size_t j = 1; size_t i = 1;

  for (;i<=a;i++){
    j = 1;
    for (;j<=b;j++){
      board[i][j] = std::min( std::min(board[i - 1][j] + 1,board[i][j - 1] + 1),
                                          board[i - 1][j - 1] + (A[i - 1] == B[j - 1] ? 0 : 1) );
                                          //cout << board[i][j] << endl;
    }

  }

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

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