Stackoverflow热门问题(四)-++i与i++有何不同?

stackoverflow热门问题目录

经过三次修改格式的劳累,我体会到了重复性劳动的痛苦。
所以这次是我用python爬的,并转换的。
而且这个爬虫是我一天写好的(虽然花了两天但第一天实际只是稍微摸了一下子)。
这个爬虫我后续会发文章出来,但根据目前待发的文章数量,应该要等到下个月。

++i与i++有何不同?

  • The.Anti.9 asked:
    • 在C中,++ii++有何区别?如果作为自增用在for循环中,哪个比较好?
  • Answers:
    • Mark Harrison - 1221位用户认为有用
      • ++i会将i值增加,并返回增加后的值。
      • i = 1; j = ++i; (i is 2, j is 2)
      • i++会将i值增加,并返回增加前的值。
      • i = 1; j = i++; (i is 2, j is 1)
      • 对于for循环,两个都可以用。但++i更普遍,因为K&R是这么用的(The C Programming Language)。
      • ++i而不是i++,会出现更少的错误。
      • 关于++i与i++有两条相关注释。在任何非学生项目编译器(non-student-project compiler)中,它们效率一致。可以试着观察编译器对其生产的代码,很容易证实这点。
        • 译者注:这里的non-student-project compiler译成非学生项目编译器似乎有点奇怪,但根据答主后续的说法,感觉这是在表达:只有被学生当成项目做的编译器,才会出现i++与++i效率不同的情况。
        • 换句话说,答主的意思应该是:但凡是个正常编译器来编译,他们效果都不会有差。
      • 效率问题的确很有趣...可以参见我在这篇的回答:Is there a performance difference between i++ and ++i in C?
        • 译者注:这里答主发的链接也能作为佐证,答主在linux中创建了一个仅i++与++i不同的for循环,并比较了编译文件的MD5,它们是一样的。
      • 正如@OnFreund指出的那样,C++的对象中,这两者是有不同的,因为operator++()是一个函数,并且编译器不知道去优化临时对象的创建,以至于产生了中间变量。
    • Parag - 202位用户认为有用
      • i++被称为后增量,++i则被称为先增量。
      • i++
      • i++是后增量,因为它会在操作结束后再对i加1.
      • 例如下面这个例子:
      • int i = 1, j;j = i++;
      • 结果是 j = 1i = 2 。这是因为i先分配给了j后,才被增加。
      • ++i
      • ++i是先增量,因为它会在操作前将i加1;换句话说,j = i会在i++执行后才执行。
      • 例如下面这个例子:
      • int i = 1, j;j = ++i;
      • 这里的值是j = 2i = 2。这是因为i的值增加后,才被分配给j。就像先执行++i再执行j = i一样。
      • 而对于题主关于其在for循环中效率的提问。答案是随便选择,因为它们会使用相同的时间。
      • for(i=0; i<5; i++) printf("%d ",i);
      • 以及
      • for(i=0; i<5; ++i) printf("%d ",i);
      • 两个循环都产生一样的输出:0 1 2 3 4。
      • 这种情况下不同:
      • for(i = 0; i<5;) printf("%d ",++i);
      • 他会输出1 2 3 4 5。
    • Andy Lester - 47位用户认为有用
      • 不用担心哪个效率更快,编译器会负责它们的。只要能保证可读性,随便用什么都可以。


What is the difference between ++i and i++?

  • The.Anti.9 asked:
    • In C, what is the difference between using ++i and i++, and which should be used in the incrementation block of a for loop?
      • 在C中,++ii++有何区别?如果作为自增用在for循环中,哪个比较好?
  • Answers:
    • Mark Harrison - 1221位用户认为有用
      • ++i will increment the value of i, and then return the incremented value.
        • ++i会将i值增加,并返回增加后的值。
      • i = 1; j = ++i; (i is 2, j is 2)
      • i++ will increment the value of i, but return the original value that i held before being incremented.
        • i++会将i值增加,并返回增加前的值。
      • i = 1; j = i++; (i is 2, j is 1)
      • For a for loop, either works. ++i seems more common, perhaps because that is what is used in K&R.
        • 对于for循环,两个都可以用。但++i更普遍,因为K&R是这么用的(The C Programming Language)。
      • In any case, follow the guideline "prefer ++i over i++" and you won't go wrong.
        • ++i而不是i++,会出现更少的错误。
      • There's a couple of comments regarding the efficiency of ++i and i++. In any non-student-project compiler, there will be no performance difference. You can verify this by looking at the generated code, which will be identical.
        • 关于++i与i++有两条相关注释。在任何非学生项目编译器(non-student-project compiler)中,它们效率一致。可以试着观察编译器对其生产的代码,很容易证实这点。
          • 译者注:这里的non-student-project compiler译成非学生项目编译器似乎有点奇怪,但根据答主后续的说法,感觉这是在表达,只有被学生当成项目做的编译器,才会出现i++与++i效率不同的情况。
          • 换句话说,答主的意思应该是:但凡是个正常编译器来编译,他们效果都不会有差。
      • The efficiency question is interesting... here's my attempt at an answer:Is there a performance difference between i++ and ++i in C?
        • 效率问题的确很有趣...可以参见我在这篇的回答:Is there a performance difference between i++ and ++i in C?
          • 译者注:这里答主发的链接也能作为佐证,答主在linux中创建了一个仅i++与++i不同的for循环,并比较了编译文件的MD5,它们是一样的。
      • As @OnFreund notes, it's different for a C++ object, since operator++() is a function and the compiler can't know to optimize away the creation of a temporary object to hold the intermediate value.
        • 正如@OnFreund指出的那样,C++的对象中,这两者是有不同的,因为operator++()是一个函数,并且编译器不知道去优化临时对象的创建,以至于产生了中间变量。
    • Parag - 202位用户认为有用
      • i++ is known as Post Increment whereas ++i is called Pre Increment.
        • i++被称为后增量,++i则被称为先增量。
      • i++
      • i++ is post increment because it increments i's value by 1 after the operation is over.
        • i++是后增量,因为它会在操作结束后再对i加1.
      • Lets see the following example:
        • 例如下面这个例子:
      • int i = 1, j;j = i++;
      • Here value of j = 1 but i = 2. Here value of i will be assigned to j first then i will be incremented.
        • 结果是 j = 1i = 2 。这是因为i先分配给了j后,才被增加。
      • ++i
      • ++i is pre increment because it increments i's value by 1 before the operation.It means j = i; will execute after i++.
        • ++i是先增量,因为它会在操作前将i加1;换句话说,j = i会在i++执行后才执行。
      • Lets see the following example:
        • 例如下面这个例子:
      • int i = 1, j;j = ++i;
      • Here value of j = 2 but i = 2. Here value of i will be assigned to j after the i incremention of i.Similarly ++i will be executed before j=i;.
        • 这里的值是j = 2i = 2。这是因为i的值增加后,才被分配给j。就像先执行++i再执行j = i一样。
      • For your question which should be used in the incrementation block of a for loop? the answer is, you can use any one.. doesn't matter. It will execute your for loop same no. of times.
        • 而对于题主关于其在for循环中效率的提问。答案是随便选择,因为它们会使用相同的时间。
      • for(i=0; i<5; i++) printf("%d ",i);
      • And
        • 以及
      • for(i=0; i<5; ++i) printf("%d ",i);
      • Both the loops will produce same output. ie 0 1 2 3 4.
        • 两个循环都产生一样的输出:0 1 2 3 4。
      • It only matters where you are using it.
        • 这种情况下不同:
      • for(i = 0; i<5;) printf("%d ",++i);
      • In this case output will be 1 2 3 4 5.
        • 他会输出1 2 3 4 5。
    • Andy Lester - 47位用户认为有用
      • Please don't worry about the "efficiency" (speed, really) of which one is faster. We have compilers these days that take care of these things. Use whichever one makes sense to use, based on which more clearly shows your intent.
        • 不用担心哪个效率更快,编译器会负责它们的。只要能保证可读性,随便用什么都可以。

版权声明:
作者:MWHLS
链接:https://mwhls.top/2143.html
来源:无镣之涯
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
< <上一篇
下一篇>>