为什么i ++而不是++ i for for循环 [英] why i++ instead of ++i in for loops

查看:86
本文介绍了为什么i ++而不是++ i for for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我一直在阅读C / C ++和Java中的各种文本。 for lops all

按以下方式运行:


int i;


for(i = 0 ; i< 4; i ++)


{


.....


}


我理解++ i和i ++之间的区别,但我不明白为什么

i ++在这些循环中使用的时候,据我所知,在这种情况下,步骤

表达式会更加理解为i = i + 1。 i ++的

逻辑是不一样的。最后的结果可能是评估

过程不是。


好​​的我在循环结束时意识到i的值是相同的

你使用过的方法。但这并没有解释为什么偏好。


也可以从我在汇编代码级别看到的内容


int i;


for(i = 0; i< 4; ++ i)


{


。 ....


}





int i;


for(i = 0; i< 4; i ++)


{


.....


}


完全一样。


有些人请说明需要使用i ++ over ++ i。

-

通过发表http://dbforums.com

推荐答案

* snip *
* snip *
请一些请解释需要使用i ++ over ++ i。
Could some please explain the need to use i++ over ++i.




严格来说,没有真正需要i ++,因为int x = i;

++ i;"产生与int x = i ++;相同的结果。你已经注意到了

for循环使用int作为循环变量使用++ i或i ++并没有与大多数编译器产生

的差异。请注意,对于迭代器,++ i版本

可能比i ++更有效。


-

Peter van Merkerk

peter.van.merkerk(at)dse.nl




Strictly speaking there is no real need for i++, since "int x = i;
++i;" yields the same result as "int x = i++;". As you have noticed in
for loops with int as loop variable using ++i or i++ doesn''t make a
difference with most compilers. Note that with iterators the ++i version
may very well be more efficient than i++.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl



newtothis写道:
newtothis wrote:

我一直在阅读C / C ++和Java中的各种文本。对于lops all
运行如下:

int i;

for(i = 0; i< 4; i ++)

{

....



我理解++ i和i ++之间的区别,但我看不到为什么我会在这些循环中使用i ++,正如我所理解的那样,在这种情况下,步骤
表达式会更加理解为i = i + 1。 i ++的逻辑是不一样的。最终的结果可能是
评估过程不是。

I have been reading various texts in C/ C++ and Java. The for lops all
run along the lines of :

int i ;

for(i = 0 ; i < 4 ; i++)

{

....

}

I understand the difference between ++i and i++, but I can not see why
i++ is used in these loops when, as I understand it, the steping
expression would be more alined to i = i + 1 in this type of case. The
logic of i++ is not the same. The final result might but the
evaluation process is not.




对于内置类型,它真的没关系。但是在C ++中,您可以为自己的类编写

和operator ++。然后它可能很重要,因为

postfix ++必须创建一个对象的副本,以便返回旧的值

。如果您不需要返回值,则该副本不需要
。如果编译器没有进行命名返回值

优化,那么该副本甚至可能需要再次复制,并且所有

只是为了抛弃结果。自己的

类的后缀运算符++可能如下所示:

MyClass MyClass :: operator ++(int)

{

MyClass retval(* this); //复制对象

//执行增量所需的任何操作对象

reutrn retval; //按值返回副本

}


前缀++可能如下所示:


MyClass& MyClass :: operator ++()

{

//执行增量所需的任何操作对象

返回* this; //返回对象的引用

}


因此,如果

不需要返回值。



For builtin types, it really doesn''t matter. But in C++, you can write
and operator++ for your own class. And then it might matter, becaure
postfix ++ has to create a copy of the object so that the old value can
be returned. If you don''t need the return value, that copy is
unnecessary. If the compiler doesn''t do named return value
optimization, that copy might even need to be copied again, and all
that just to throw the result away. The postfix operator++ for an own
class might look something like this:

MyClass MyClass::operator++(int)
{
MyClass retval(*this); // copy the object
// do whatever is needed to "increment" the object
reutrn retval; // return the copy by value
}

while prefix ++ might look like:

MyClass& MyClass::operator++()
{
// do whatever is needed to "increment" the object
return *this; // return a refernce to the object
}

Therefore, it''s considered a good habit to always use prefix ++ if the
return value is not needed.


newtothis写道:
newtothis wrote:
我一直在阅读C / C ++和Java中的各种文本。对于lops all
运行如下:

int i;

for(i = 0; i< 4; i ++)

{
....
}
我理解++ i和i ++之间的区别,但我看不出为什么使用了i ++根据我的理解,在这些循环中,在这种情况下,步骤
表达式会更加理解为i = i + 1。 i ++的逻辑是不一样的。最终的结果可能是评估
过程不是。


好​​的电话,newtothis!

好的我在循环结束时意识到i的价值是相同的
你使用的方法。但这并没有解释为什么偏好。

也可以从我在汇编代码级别看到的内容

int i;

= 0; i< 4; ++ i)
{
....
}



int i;

for(i = 0; i< 4; i ++)
{
....
}

正好是同样。

哪个,本身是无关紧要的 - 只是因为编译器可以看到'i ++'的返回值,即'i ++'的原始值,即`i的原始值''是

抛出

离开。
请一些人解释一下使用i ++ over ++ i的必要性。

对不起。不能做。 ;-)没有需要。

有什么*是*是习惯的力量。历史神器。自定义。成语。


虽然在处理基本类型时,

生成的代码可能没有区别,但通常*是* a

差异 - 一个可能很重要的 - 在处理

用户定义的类型时(其中`++'',前缀和后缀超载

运营商)。


再次,好的电话。在这样的循环中使用前缀形式是首选 - 这只是一个很难打破的习惯!


HTH,

--ag

-
通过 http:/发表/dbforums.com
I have been reading various texts in C/ C++ and Java. The for lops all
run along the lines of :

int i ;

for(i = 0 ; i < 4 ; i++)

{
....
}

I understand the difference between ++i and i++, but I can not see why
i++ is used in these loops when, as I understand it, the steping
expression would be more alined to i = i + 1 in this type of case. The
logic of i++ is not the same. The final result might but the evaluation
process is not.
Good call, "newtothis"!

Ok I realise at the end of the loop the value of i is the same which
ever method you use. But that does not explaine why the preference.

Also from what I can see at the assembly code level

int i ;

for(i = 0 ; i < 4 ; ++i)
{
....
}

and

int i ;

for(i = 0 ; i < 4 ; i++)
{
....
}

are exactly the same.
Which, by itself is irrelevant -- and only because a compiler can see
that the return value of `i++'', i.e. the original value of `i'' is
thrown
away.
Could some please explain the need to use i++ over ++i.
Sorry. Can''t be done. ;-) There is no `need''.
What there *is* is `force of habit''. Historical artifact. Custom. Idiom.

While it is true that there is likely to be no difference in the
generated code when dealing with basic types, there often *is* a
difference -- and a potentially significant one -- when dealing with
user defined types (where `++'', prefix and postfix are overloaded
operators).

Again, good call. Using the prefix form in such loops is to be
preferred -- it''s just a hard habit to break!

HTH,
--ag

--
Posted via http://dbforums.com




-

Artie Gold - 德克萨斯州奥斯汀

哦,对于常规老垃圾邮件的美好时光。



--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.


这篇关于为什么i ++而不是++ i for for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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