这是更好的"的for(int i = 0; i = 5;!++ I)QUOT;或QUOT;的for(int i = 0; I< = 5;我++)QUOT ;? [英] which is better "for(int i = 0; i != 5; ++i)" or "for(int i = 0; i <= 5; i++)"?

查看:176
本文介绍了这是更好的"的for(int i = 0; i = 5;!++ I)QUOT;或QUOT;的for(int i = 0; I< = 5;我++)QUOT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是更好的的for(int i = 0;!我= 5; ++ I)的for(int i = 0;我< = 5;?我++)

请解释的理由,如果可能的话。

Please explain the rationale, if possible.

我读的地方,!=运算符比比较好操作。还$ P $对增量操作者比递增运算符更好,因为它不要求任何临时变量存储中间值

I read somewhere that != operator is better than comparison operators. also pre-increment operator is better than post-increment operator, since it doesn't require any temporary variable to store the intermediate value.

有没有for循环比这两个更好的形式?

Is there any better form of for loop than these two?

P.S:我用的是前者通过的来源,我现在不记得有一个的建议。

p.s: I use the former one as suggested by one of the sources, which i don't remember now.

推荐答案

有至少有两点不同:


  • 第一个将迭代的5倍(从0到4),而第二个将迭代6次(从0到5)。

  • the first one will iterate 5 times (from 0 to 4), while the second one will iterate 6 times (from 0 to 5).

这是一个逻辑的差异,这取决于你需要做什么。

This is a logic difference, and it depends on what you need to do.

如果你的意思的第二个例子是 I< = 4 (!对 I = 5 )你不应该打扰:任何编译器总是能够优化甚至与优化,关闭这样的小事前pression

If what you meant for the second example was i<=4 (versus i!=5) you shouldn't bother: any compiler will always be able to optimize such a trivial expression even with optimizations turned off.

第二个区别是使用运营商的 ++ :在一个情况下,你使用preFIX版本,而其他后缀版本。

the second difference is the use of operator ++: in a case you use the prefix version, while in the other the postfix version.

这不会使本机类型的差异,但可以做用户定义类型存在差异(如类,结构,枚举,...),因为用户可以重载运营商,而后缀一( VAR ++ )可能有时慢一点。

This doesn't make difference for native types, but could do some difference with user defined types (ie classes, structs, enums, ...), since the user could overload the operator, and the postfix one (var ++) could be a little slower sometimes.

这篇关于这是更好的&QUOT;的for(int i = 0; i = 5;!++ I)QUOT;或QUOT;的for(int i = 0; I&LT; = 5;我++)QUOT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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