++ n和n ++有何不同?

编程入门 行业动态 更新时间:2024-10-27 00:25:23
本文介绍了++ 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 ++有何不同?

本文发布于:2023-11-22 08:36:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616714.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有何不同

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!