Chinaunix首页 | 论坛 | 博客
  • 博客访问: 475429
  • 博文数量: 51
  • 博客积分: 1056
  • 博客等级: 少尉
  • 技术积分: 676
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-10 22:53
文章存档

2018年(2)

2017年(1)

2013年(2)

2012年(9)

2011年(36)

2010年(1)

分类: C/C++

2011-05-24 14:29:10


lz问:
我们一般都是这样写的for(i=0;i书上又是这样写的for(i=0;i回答:
下面的引用自C++ Primer4:
Advice: Use Postfix Operators Only When Necessary 建议:只有在必要时才使用后置操作符 Readers from a C background might be surprised that we use the prefix increment in the programs we've written. The reason is simple: The prefix version does less work. It increments the value and returns the incremented version. The postfix operator must store the original value so that it can return the unincremented value as its result. For ints and pointers, the compiler can optimize away this extra work. For more complex iterator types, this extra work potentially could be more costly. By habitually favoring the use of the prefix versions, we do not have to worry if the performance difference matters. 有使用 C 语言背景的读者可能会觉得奇怪,为什么要在程序中使用前自增操作。道理很简单:因为前置操作需要做的工作更少,只需加 1 后返回加 1 后的结果即可。而后置操作符则必须先保存操作数原来的值,以便返回未加 1 之前的值作为操作的结果。对于 int 型对象和指针,编译器可优化掉这项额外工作。但是对于更多的复杂迭代器类型,这种额外工作可能会花费更大的代价。因此,养成使用前置操作这个好习惯,就不必操心性能差异的问题

阅读(1340) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~