++ i或i ++在for循环? [英] ++i or i++ in for loops ??
本文介绍了++ i或i ++在for循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个原因,一些程序员在正常的for循环中写入 ++ i
,而不是写 i ++
?
解决方案
如果 i
是非平凡类的对象,则 + i
通常是首选,因为对象被修改然后计算,而 i ++
在计算后修改,因此需要进行复制。 / p>
Possible Duplicate:
Is there a performance difference between i++ and ++i in C++?
Is there a reason some programmers write ++i
in a normal for loop instead of writing i++
?
解决方案
For integers, there is no difference between pre- and post-increment.
If i
is an object of a non-trivial class, then ++i
is generally preferred, because the object is modified and then evaluated, whereas i++
modifies after evaluation, so requires a copy to be made.
这篇关于++ i或i ++在for循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文