简体   繁体   English

C++ while 循环 v[i++] vs i++ in while body

[英]C++ while loop v[i++] vs i++ in while body

I have a question about iterating in a while loop.我有一个关于在 while 循环中迭代的问题。 So let's say we have a vector with size 10 and elements are from 0-9.所以假设我们有一个大小为 10 的向量,元素从 0 到 9。 We have a for loop that just iterates once and within the for loop we have the while loop.我们有一个只迭代一次的 for 循环,在 for 循环中我们有一个 while 循环。 I am getting two different results when I print the loop and I'm unsure why.当我打印循环时,我得到了两种不同的结果,但我不确定为什么。

I understand the idea behind the first loop where we increment j in the body of the while and we don't enter the loop on the last iteration since j is 5 so we only print 0-4.我理解第一个循环背后的想法,我们在 while 的主体中增加 j 并且我们没有在最后一次迭代中进入循环,因为 j 是 5,所以我们只打印 0-4。 The second loop is where I'm having issues with understanding.第二个循环是我在理解方面遇到问题的地方。 My logic is first we increment the pointer j to 1 so that's why we cout 1 instead of 0. But once j is 4 v[j++] = 5 and in the condition in the loop breaks when v[j++] = 5 so why do we cout 5 still?我的逻辑是首先我们将指针 j 增加到 1,所以这就是我们计算 1 而不是 0 的原因。但是一旦 j 是 4 v[j++] = 5 并且在循环中的条件中,当 v[j++] = 5 时,为什么要这样做我们还是 5 吗? Any help is appreciated.任何帮助表示赞赏。

  vector<int> v(10);
  for(int i = 0; i < v.size(); i++){
    v[i] = i;
  }
  int j = 0;
  for(int i = 0; i < 1; i++){ // first loop
    while(v[j] != 5){
      cout << v[j]; //prints 0 1 2 3 4
      j++;
    }
  }
  for(int i = 0; i < 1; i++){ //second loop
    while(v[j++] != 5){
      cout << v[j]; //prints 1 2 3 4 5
    }
  }

j++ will increment the value of j but return the original value of j before being incremented. j++将递增的值j但返回的原始值j递增之前。

 int i =0;
 int j = 0;
 i = 1;
 j = i++;
 (i is 2, j is 1)

Source 来源

The postfix increment conceptually copies the operand in memory, increments the original operand, and finally yields the value of the copy.后缀递增在概念上复制内存中的操作数,递增原始操作数,最后产生副本的值。

The below snippet is fairly straightforward.下面的代码片段相当简单。 The j has been incremented after printing the content of the vector .打印vector的内容后, j已增加。

vector<int> v(10);
for(int i = 0; i < v.size(); i++){
    v[i] = i;
}
int j = 0;

for(int i = 0; i < 1; i++){ // first loop
    while(v[j] != 5){
        cout << v[j]; //prints 0 1 2 3 4
        j++;
    }
}

Now, let's take the look at the 2nd loop.现在,让我们看看第二个循环。

vector<int> v(10);
for(int i = 0; i < v.size(); i++){
    v[i] = i;
}
int j = 0;
for(int i = 0; i < 1; i++){ // second loop
    // values of j are 0 1 2 3 4
    while(v[j++] != 5){
        // values of j are 1 2 3 4 5 because it has been incremented
        cout << v[j]; //prints 1 2 3 4 5
    }
}

As it is said above, j++ will increment the j so when j is 3, v[j++] is 4 so condition in while is not broken.如上所述,j++ 会增加 j,因此当 j 为 3 时,v[j++] 为 4,因此 while 中的条件不会被破坏。 But because of j++, when cout is executed j is 4 and v[4] is 5 so 5 gets printed但是因为 j++,当 cout 被执行时 j 是 4 并且 v[4] 是 5 所以 5 被打印出来

You forgot to reinitialize j after a the first loop.您忘记在第一个循环后重新初始化j If you want to print from 1 to 5 in the second loop, then you should reinitialize j as 0 before the second loop runs.如果您想在第二个循环中从 1 打印到 5,那么您应该在第二个循环运行之前将j重新初始化为 0。 Try:尝试:

vector<int> v(10);
    for (int i = 0; i < v.size(); i++) {
        v[i] = i;
    }
    int j = 0;
    for (int i = 0; i < 1; i++) { // first loop
        while (v[j] != 5) {
            cout << v[j]; //prints 0 1 2 3 4
            j++;
        }
    }
    j = 0;
    for (int i = 0; i < 1; i++) { //second loop
        while (v[j++] != 5) {
            cout << v[j]; //prints 1 2 3 4 5
        }
    }

using a single for loop will help understand the code better since both conditions are same for the loop.使用单个 for 循环将有助于更好地理解代码,因为循环的两个条件相同。

    vector<int> v(10);
    for (int i = 0; i < v.size(); i++) {
        v[i] = i;
    }
    int j = 0;
    for (int i = 0; i < 1; i++) { // first loop
        while (v[j] != 5) {
            cout << v[j]; //prints 0 1 2 3 4
            j++;
        }
// joined the end of the first loop and the start of the second loop
        j = 0;
        while (v[j++] != 5) {
            cout << v[j]; //prints 1 2 3 4 5
        }
    }

Hope it will help in answering your question希望对回答你的问题有所帮助

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

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