++ i vs i ++ [英] ++i vs i++

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

问题描述

大家好,


我想知道什么时候应该使用++我而不是i ++,反之亦然。

一些代码示例将是帮助。


分配真的很重要吗?


谢谢,

Arut

Hi All,

I would like to know when ++i should be used instead of i++ and vice versa.
Some code examples would be of help.

Does it really matter for assignments?

Thanks,
Arut

推荐答案

asm< ar ** @ post.com>潦草地写了下面的内容:
asm <ar**@post.com> scribbled the following:
大家好,
我想知道什么时候应该使用++我而不是i ++,反之亦然。
一些代码示例会有所帮助。
分配真的很重要吗?
Hi All, I would like to know when ++i should be used instead of i++ and vice versa.
Some code examples would be of help. Does it really matter for assignments?




是的,这很重要。考虑j = ++ i和j = i ++。在这两种情况下,我都会获得

加1。但是,在前一个案例中,j的价值比后者更多一个。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰-------- \

\ - http://www.helsinki.fi/~palaste - --------------------规则! -------- /

"''可以很容易地证明'''的意思''我看过一次这样的证明(我没有这样做)
明白了)我不能再记得了''。

- 数学老师



Yes it matters. Consider j=++i and j=i++. In both cases i gets
incremented by one. However, the value of j is one more in the former
case than in the latter case.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"''It can be easily shown that'' means ''I saw a proof of this once (which I didn''t
understand) which I can no longer remember''."
- A maths teacher


asm写道:
asm wrote:
我想知道什么时候应该使用++ i而不是i ++,反之亦然。


当你想要增加的i值时使用++ i,当你想要i的原始值时,使用i ++。


如果你不在乎你得到的价值,可以使用其中任何一个(尽管我将

切换到'i + = 1`)。

分配是否真的重要?
I would like to know when ++i should be used instead of i++ and vice
versa.
Use ++i when you want the incremented value of i, and i++ when you
want the original value of i.

If you don''t care which value you get, use either (although then I
switch to `i += 1`).
Does it really matter for assignments?




是的,这真的很重要。


-

克里斯电刺猬 Dollin

C常见问题解答: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html

C欢迎: http://www.angelfire.com/ms3/bchambl...me_to_clc.html


ar**@post.com (asm)写道:
ar**@post.com (asm) writes:
大家好,

我想知道什么时候应该使用++我而不是i ++,反之亦然。
一些代码示例将是帮助。
Hi All,

I would like to know when ++i should be used instead of i++ and vice versa.
Some code examples would be of help.




就速度而言,存在差异。如果你看一下迭代器:

它们是高级对象,支持post(i ++)和

前缀(++ i)表单,但对于postfiz表单孔对象需要复制两次,一次用于存储旧值,另一次用于返回旧值。$ / b $ b返回旧值。


恕我直言,如果你不需要后缀形式的行为,请使用前缀

表格。


种类规则,

Nicolas


-

| Nicolas Pavlidis | Elvis Presly:| \ | __ |

| SE& s的学生KM | 进入goto | \ | __ | |

| pa****@sbox.tugraz.at | ICQ#320057056 | |

| -------------------格拉茨技术大学---------------- |



In terms of speed there is a difference. If you look at iterators:
They are highlevel objects that support the ++ in post(i++) and
prefix(++i) form, but for the postfiz form the hole object needs to be
copied two times, one time for storing the old value and another tie for
returning the old value.

IMHO if you don''t need the behaviour of the postfix form, use the prefix
form.

Kind regrads,
Nicolas

--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| pa****@sbox.tugraz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|


这篇关于++ i vs i ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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