C++ 循环(for/while)中的前增量和后增量有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17366847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between pre-increment and post-increment in the cycle (for/while)?
提问by Kulvar
My interest is in the difference between for
and while
loops. I know that the post-increment value is used and then incremented and the operation returns a constant pre-increment.
我的兴趣在于循环for
和while
循环之间的区别。我知道使用后增量值然后增加并且操作返回一个恒定的预增量。
while (true) {
//...
i++;
int j = i;
}
Here, will j
contain the old i
or the post-incremented i
at the end of the loop?
在这里,将在循环结束时j
包含旧的i
还是后递增i
的?
回答by zennehoy
Since the statement i++ ends at the ;
in your example, it makes no difference whether you use pre- or post-increment.
由于语句 i++;
在您的示例中以 the 结尾,因此无论您使用前增量还是后增量都没有区别。
The difference arises when you utilize the result:
当您使用结果时会出现差异:
int j = i++; // i will contain i_old + 1, j will contain the i_old.
Vs:
对比:
int j = ++i; // i and j will both contain i_old + 1.
回答by Tim
Depends on how you use them.
取决于你如何使用它们。
i++
makes a copy, increases i, and returns the copy (old value).++i
increases i, and returns i.
i++
制作一个副本,增加 i,并返回副本(旧值)。++i
增加 i,并返回 i。
In your example it is all about speed. ++i
will be the faster than i++
since it doesn't make a copy.
在您的示例中,一切都与速度有关。++i
将比i++
因为它不制作副本更快。
However a compiler will probably optimize it away since you are not storing the returned value from the increment operator in your example, but this is only possible for fundamental types like a int
.
但是,编译器可能会对其进行优化,因为您没有在示例中存储增量运算符的返回值,但这仅适用于int
.
回答by Kulvar
Basic answer for understanding. The incrementation operator works like this:
理解的基本答案。增量运算符的工作方式如下:
// ++i
function pre_increment(i) {
i += 1;
return i;
}
// i++
function post_increment(i) {
copy = i;
i += 1;
return copy;
}
A good compiler will automatically replace i++
with ++i
when it detect that the returned value will not be used.
一个好的编译器会自动更换i++
与++i
当检测到返回的值将不会被使用。
回答by Pbk1303
In pre-increment the initial value is first incremented and then used inside the expression.
在预递增中,初始值首先递增,然后在表达式中使用。
a = ++i;
In this example suppose the value of variable i
is 5. Then value of variable a
will be 6 because the value of i
gets modified before using it in a expression.
在这个例子中,假设变量的i
值为 5。那么变量a
的值为 6,因为i
在表达式中使用它之前,变量的值被修改了。
In post-increment value is first used in a expression and then incremented.
在后增量中,值首先在表达式中使用,然后再增加。
a = i++;
In this example suppose the value of variable i
is 5. Then value of variable a
will be 5 because value of i
gets incremented only after assigning the value 5 to a
.
在这个例子中,假设变量的i
值为 5。那么变量的值a
将是 5,因为i
只有在将值 5 分配给 之后,变量的值才会增加a
。
回答by Taha
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argp)
{
int x = 5;
printf("x=%d\n", ++x);
printf("x=%d\n", x++);
printf("x=%d\n", x);
return EXIT_SUCCESS;
}
Program Output:
程序输出:
x=6
x=6
x=7
In the first printf statement x is incremented before being passed to printf so the value 6 is output, in the second x is passed to printf (so 6 is output) and then incremented and the 3rd printf statement just shows that post increment following the previous statement by outputting x again which now has the value 7.
在第一个 printf 语句中,x 在传递给 printf 之前递增,因此输出值 6,在第二个中 x 传递给 printf(因此输出 6)然后递增,第三个 printf 语句仅显示前一个后递增通过再次输出 x 现在具有值 7 的语句。
回答by Naira Magdy
i++ uses i's value then increments it but ++i increments i's value before using it.
i++ 使用 i 的值然后增加它但 ++i 在使用它之前增加 i 的值。
回答by Oswin Topno
it does not matter if you use pre or post increment in an independent statement, except for the pre-increment the effect is immediate
在独立语句中使用 pre 或 post increment 无关紧要,除了 pre-increment 效果是立竿见影的
//an example will make it more clear:
int n=1;
printf("%d",n);
printf("%d",++n);// try changing it to n++(you'll get to know what's going on)
n++;
printf("%d",n);
output: 123
输出:123
回答by Arnav Borborah
The difference between post- and pre-increment is really, in many cases subtle. post incremenet, aka num++
, first creates a copy of num
, returns it, and after that, increments it. Pre-increment, on the other hand, aka ++num
, first evaluates, then returns the value. Most modern compilers, when seeing this in a loop, will generally optimize, mostly when post increment is used, and the returned initial value is not used. The most major difference between the 2 increments, where it is really common to make subtle bugs, is when declaring variables, with incremented values: Example below:
在许多情况下,post-increment 和 pre-increment 之间的区别确实很微妙。post Incremenet,又名num++
,首先创建 的副本num
,返回它,然后增加它。另一方面,预增量又名++num
,首先计算,然后返回值。大多数现代编译器在循环中看到这一点时,通常会进行优化,主要是在使用后增量时,并且不使用返回的初始值。2 次增量之间的最大区别,在这两个增量中,制造细微错误非常常见,是在声明具有增量值的变量时:示例如下:
int num = 5;
int num2 = ++num; //Here, first num is incremented,
//then made 6, and that value is stored in num2;
Another example:
另一个例子:
int num = 5;
int num2 = num++; //Here, num is first returned, (unfortunately?), and then
//incremented. This is useful for some cases.
The last thing here I want to say is BE CAREFUL WITH INCREMENTS. When declaring variables, make sure you use the right increment, or just write the whole thing out (num2 = num + 1
, which doesn't always work, and is the equivalent of pre-increment). A lot of trouble will be saved, if you use the right increment.
我想说的最后一件事是小心增量。声明变量时,请确保使用正确的增量,或者只写出整个内容(num2 = num + 1
,这并不总是有效,并且相当于预增量)。如果您使用正确的增量,将节省很多麻烦。