C ++;我和我++;?
在C语言中,使用C ++;我和我++;?,c,for-loop,post-increment,pre-increment,C,For Loop,Post Increment,Pre Increment,在C语言中,使用++i和i++之间有什么区别,在for循环的递增块中应该使用哪一个?++i递增值,然后返回它 int j=0; System.out.println(j); //0 System.out.println(j++); //0. post-increment. It means after this line executes j increments. int k=0; System.out.println(k); //0 System.out.println(++k); //1
++i
和i++
之间有什么区别,在for
循环的递增块中应该使用哪一个?++i
递增值,然后返回它
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
i++
返回值,然后递增
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
这是一个微妙的区别
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
对于For循环,使用++i
,因为它稍微快一点i++
将创建一个刚刚被丢弃的额外副本。
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
++i
将递增i
的值,然后返回递增的值
i = 1;
j = ++i;
(i is 2, j is 2)
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
i++
将增加i
的值,但返回在增加之前保留的原始值
i = 1;
j = i++;
(i is 2, j is 1)
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
对于For
循环,两种方法都可以<代码>++i
似乎更为常见,可能是因为这正是在中使用的
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
在任何情况下,请遵循“首选++i
而不是i++
”的指导原则,这样您就不会出错
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
关于++i
和i++
的效率,有一些评论。在任何非学生项目编译器中,都不会有性能差异。您可以通过查看生成的相同代码来验证这一点
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
效率问题很有趣。。。以下是我试图回答的问题:
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
<>作为注释,对于C++对象来说是不同的,因为<代码>操作符++()是一个函数,编译器不知道如何优化创建一个临时对象来保存中间值。原因是
++i
比i++
稍快一点,因为i++
可以在i的值增加之前需要i的本地副本,而++i
从不这样做。在某些情况下,一些编译器会尽可能地优化它。。。但这并不总是可能的,并非所有的编译器都这样做
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
我尽量不太依赖编译器优化,所以我会遵循Ryan Fox的建议:当我可以同时使用这两种方法时,我会使用
++I
请不要担心哪一个更快的“效率”(速度,真的)。现在我们有编译器来处理这些事情。使用任何一个有意义的选项,在此基础上更清楚地显示您的意图。在循环中使用任何一个选项的有效结果都是相同的。换句话说,在这两种情况下,循环将执行完全相同的操作
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
就效率而言,选择i++而不是++i可能会受到惩罚。就语言规范而言,使用增量后操作符应该创建操作符所作用的值的额外副本。这可能是额外操作的来源
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
但是,你应该考虑前面逻辑中的两个主要问题:
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
在我看来,整个问题归结为一种风格偏好。如果您认为pre-increment更具可读性,那么就使用它。就我个人而言,我更喜欢post incrment,但这可能是因为这是我在了解优化之前所学的
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
这是一个典型的过早优化的例子,像这样的问题有可能分散我们对设计中严重问题的注意力。然而,这仍然是一个很好的问题,因为“最佳实践”中的用法或共识并不一致。我想你现在理解了语义上的差异(尽管老实说,我想知道为什么)
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
人们在堆栈溢出问题上提出“运算符X是什么意思”的问题,而不是阅读,
你知道,一本书或网络教程之类的
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
但无论如何,关于使用哪一个,忽略性能问题,它们是
即使在C++中也不太重要。这是你在决定时应该使用的原则。
要使用的名称:
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
用代码说出你的意思
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
如果在语句中不需要递增前的值,请不要使用这种形式的运算符。这是一个小问题,但除非您使用的是禁止递增的样式指南
完全支持另一个版本(也称为骨头样式指南),您应该使用
最准确地表达你想做什么的形式
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
QED,使用增量前版本:
for (int i = 0; i != X; ++i) ...
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
i++称为增量后,而++i称为增量前。
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
int m=0;
if((m == 0 || m++ == 0) && (m++ == 1)) { //false
/* in OR condition if first line is already true then compiler doesn't check the rest. It is technique of compiler optimization */
System.out.println("post-increment "+m);
}
int n=0;
if((n == 0 || n++ == 0) && (++n == 1)) { //true
System.out.println("pre-increment "+n); //1
}
i++
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
i++
是后增量,因为它在操作结束后将i
的值增加1
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
让我们看看下面的示例:
int i = 1, j;
j = i++;
int i = 1, j;
j = ++i;
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
此处j=1
的值,但i=2
。此处i
的值将首先分配给j
,然后i
将递增
i = 1;
j = i++;
(i is 2, j is 1)
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
++i
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
++i
是预增量,因为它在操作之前将i
的值增加1。
这意味着j=i;
将在i++
之后执行
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
让我们看看下面的示例:
int i = 1, j;
j = i++;
int i = 1, j;
j = ++i;
int j=0;
System.out.println(j); //0
System.out.println(j++); //0. post-increment. It means after this line executes j increments.
int k=0;
System.out.println(k); //0
System.out.println(++k); //1. pre increment. It means it increments first and then the line executes
此处j=2
的值,但i=2
。此处i
的值将分配给