++ n和n ++有何不同? [英] how ++n and n++ differ ?

查看:74
本文介绍了++ n和n ++有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i ++和++ i之间的基本区别是什么?

what is the basic difference between i++ and ++i ? which is better?

推荐答案

n = ++ i; //#1:将i加1后将i分配给n
n = i ++; //#2:在将i加1之前将i分配给n

哪个更好?

++ i应该比i ++更受青睐;我相信Stroustrup,Myers和其他人已经在各种书籍和/或帖子中对此进行了陈述.

我们之所以选择C语言(尤其是图形语言),是因为编译器创建了一个临时(在汇编级别)来保存i的先前值;在将i加1之后,将该临时对象分配给n.因此,效率较低.

对于内置类型,现代编译器通常会优化临时文件;尽管不需要这样做.

在C ++中,养成更喜欢++ i的习惯就显得尤为重要.原因是操作员超载.除非该类非常简单,否则编译器无法优化出临时类;这可能是非常昂贵的.您不想意外地创建不必要的开销,因为您习惯于编写i ++.

只需创建一个简单的类并覆盖前后的增量,然后逐步逐步解决问题即可.

在介绍代码示例时,大多数现代书籍始终使用i ++.这很不好,因为99%的时间都可以使用++ i.

for(i = 0; i< 10; ++ i){}//好习惯
for(i = 0; i< 10; i ++){}//不良习惯
n = ++i; // #1 : assigns i to n after adding 1 to i
n = i++; // #2 : assigns i to n before adding 1 to i

which is better?

++i should be preferred over i++; I believe Stroustrup, Myers and others have stated this in various books and/or post.

The reason we preferred this in C (especially for graphics), was that the compiler created a temporary (at assembly level) to hold the previous value of i; the temporary was then assigned to n after adding 1 to i. Therefore, it was less efficient.

For built-in types a modern compiler will normally optimize out the temporary; although they are not required to do so.

In C++ it is even more important to get in the habit of preferring ++i. The reason for this is operator overloading; which, unless the class is very simple, the compiler cannot optimize out the temporary; which can be very costly. You do not want to accidentally create unneeded overhead because you are in the habit of writing i++.

Just create a simple class and override the pre and post increments, then step through it to see the problem.

Most modern books consistently use i++ when presenting code examples. This is bad, because 99% of the time ++i could have been used instead.

for( i=0; i<10; ++i ) {} // Good habit
for( i=0; i<10; i++ ) {} // Bad habit


1. i ++将递增i并返回i值.
2. i + 1是不会影响i并返回i + 1值的右值.
3. ++ i,使i递增并返回i + 1值;

例如:
1. i++ will increment i and returns the i value.
2. i+1 is rvalue that wont effect i and returns i+1 value.
3. ++i, increments i and returns i+1 value;

example:
int i = 9;
int j;
j = i++; //After this statement j value will be 9, i value will be 10;
j = i+1; //After this statement j value will be 10, i value will be 9;
j = ++i; //After this statement j value will be 10, i value will be 10;



因此通常在i++;++i;之类的独立表达式中没有区别.但是区别是在您在assign语句中使用它们时(如示例所示);



so normally in stand alone expressions like i++; and ++i; have no difference.but the difference comes when you use them in assign statements(as shown in example);


try:
try:
int i = 5;
int a = i++;
printf("%d\n", a);



vs



vs

int i = 5;
int a = ++i;
printf("%d\n", a);




提示:第一个片段将输出5,第二个片段将输出6.

或者,您甚至可以尝试阅读文档(不会对您造成伤害).




Hint: first snippet will output 5, second one will output 6.

Or you may even try reading the documentation (it won''t hurt you).


这篇关于++ n和n ++有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆