本文介绍了循环问题的基础的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




之间是否存在差异(int i = 0;某些条件; i ++)

{....}





for(int i = 0;某些条件; ++ i)?

{....}


似乎没有,通过一些实验,但我认为值得

检查因为j ++和++ j通常意味着不同的东西在

语言++ c。


非常感谢,


Paul Epstein


解决方案



你的意思是......性能,语义,...的差异??


从语义上看,存在差异,因为在第一种情况下

临时对象被创建。

速度和性能可能有差异,也可能没有差别。它取决于你使用的C ++实现,优化等等的



干杯

-

Mateusz Loskot




你的意思是......性能,语义,......的差异??


从语义上看,存在差异,因为在第一种情况下

临时对象被创建。

速度和性能可能有差异,也可能没有差别。它取决于您使用的C ++实现,优化等等



一般来说(和Cline等人的常见问题解答书同意: Q23.08),则应该使用前缀增量。原理是

,后缀运算符需要复制操作数,

递增操作数,然后返回副本的值;前缀

运算符只需要递增操作数并返回其值。



你的意思是......性能,语义,......的差异?在语义上,有区别因为在第一种情况下临时
对象已创建。
速度和性能可能有差异,也可能没有差别。它取决于您使用的C ++实现,优化等。



一般来说(和Cline等人的常见问题解答书同意:Q23.08),

如果操作数

的先前值被丢弃,则应该使用前缀增量,如上例所示。理由是

后缀运算符需要复制操作数,增加

操作数,然后返回副本的值;前缀运算符只是

需要递增操作数并返回其值。



出于兴趣,标准要求后缀版本是否需要* b $ b * *来制作副本?显然,它不会为int产生任何

的差异(我希望优化编译器在这里生成

相同的代码),但是在增量时一个具有非平凡的

复制构造函数的对象,它可能会有所作为。


-

Lionel B

Is there any difference between

for(int i = 0; some condition; i++)
{....}

and

for(int i = 0; some condition; ++i) ?
{....}

It seems not, by some experimenting, but I thought it was worth
checking since j++ and ++j usually mean different things in the
language ++c .

Thanks a lot,

Paul Epstein


解决方案

You mean difference in...performance, semantic,...?

Semantically, there is a difference because in the first case
temporary object is created.
There may or may not be difference in speed and performance. it depends
on C++ implementation you use, optimization, etc.

Cheers
--
Mateusz Loskot
http://mateusz.loskot.net



You mean difference in...performance, semantic,...?

Semantically, there is a difference because in the first case
temporary object is created.
There may or may not be difference in speed and performance. it depends
on C++ implementation you use, optimization, etc.

Generally speaking (and the FAQ book from Cline et al concurs:
Q23.08), one should use the prefix increment if the previous value of
the operand is discarded, as in the example above. The rationale is
that the postfix operator needs to make a copy of the operand,
increment the operand, then return the value of the copy; the prefix
operator merely needs to increment the operand and return its value.



You mean difference in...performance, semantic,...?

Semantically, there is a difference because in the first case temporary
object is created.
There may or may not be difference in speed and performance. it depends
on C++ implementation you use, optimization, etc.


Generally speaking (and the FAQ book from Cline et al concurs: Q23.08),
one should use the prefix increment if the previous value of the operand
is discarded, as in the example above. The rationale is that the
postfix operator needs to make a copy of the operand, increment the
operand, then return the value of the copy; the prefix operator merely
needs to increment the operand and return its value.

Out of interest, does the standard mandate that the postfix version is
*required* to make a copy? Obviously it''s not going to make any
difference for an int (and I''d expect an optimising compiler to generate
identical code here), but when incrementing an object with a non-trivial
copy constructor it potentially could make a difference.

--
Lionel B


这篇关于循环问题的基础的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 16:05