增量、前增量和后增量
请帮我解决这个问题。该表达式遵循的步骤是:
//Expression
offSpring1[m1++] = temp1;
//步骤:
1.- 增加 m1
2.- 将 temp1 分配给 offSpring
我一直认为括号内的表达式是首先要做的。但现在我很困惑。所以如果这样写:
//Expression
offSpring1[++m1] = temp1;
//步骤是: 1.- 将 temp1 分配给 offSpring 2.- 增加 m1
如果步骤与第一个相同,那么 i++ 和 ++i 之间有什么区别?
Help me to resolve this please. The steps that follows that expressions are:
//Expression
offSpring1[m1++] = temp1;
//Steps:
1.- increment m1
2.- assign temp1 to offSpring
I have always thought that the expression inside the brackets was the first to be done. But now I am confuse. So if a write this:
//Expression
offSpring1[++m1] = temp1;
//Steps would be: 1.- assign temp1 to offSpring 2.- increment m1
If the steps would be the same as first ones, what is the difference between i++ and ++i?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
输出:
i++
返回表达式中当前的值,然后递增变量。++i
将递增变量,然后返回要在当前表达式中使用的值。output:
i++
returns the value as it currently stands in the expression, then increments the variable.++i
will increment the variable, then return the value to use in the current expression.是
并且
是
is
and
is
j = ++i
与i = i+1 相同; j = i;
j = i++
与j = i; 相同我=我+1;
j = ++i
is the same asi = i+1; j = i;
j = i++
is the same asj = i; i = i+1;
只需运行这两个不同的测试程序即可了解后自增和预自增运算符之间的差异
对于 ++i(前自增)
在第一个程序中,您将得到 10 作为值关闭弹簧[m1]。
为什么?因为这是预递增运算符,这意味着第一个 m1 递增,其余的进行评估。
对于 i++(后增量)
在第二个中,因为使用了后增量运算符,您将得到 0 值,因为您首先将 10 分配给 offSpring[m1],然后 m1 递增。
Just run these two different test programs to understand the difference between the post-increment and the pre-increment operators
For ++i (pre-increment)
In the first one you will get 10 as the value of offSpring[m1].
Why? Because this is the pre-increment operator which means that first m1 gets incremented and the the rest gets evaluated.
For i++(post-increment)
In the second because the post-increment operator is used you will get a 0 value since you are first assigning 10 to offSpring[m1] and then m1 gets incremented.
offSpring1[m1++] = temp1;
不执行您所说的操作。offSpring1[temp_m1]
temp1
分配给索引值。另一方面,offSpring1[++m1] = temp1; 的工作原理如下:
offSpring1[m1]
temp1
分配给索引值。offSpring1[m1++] = temp1;
doesn't do what you said.offSpring1[temp_m1]
temp1
into indexed value.On the other hand
offSpring1[++m1] = temp1;
works like this:offSpring1[m1]
temp1
into indexed value.尽管后缀增量是第一个示例中第一个被评估的,但它的值是被增量的变量的原始值。
因此,即使 m1 在数组索引之前递增,
temp1
的值也会分配到位置m1 - 1
处。Even though postfix increment is the first to be evaluated in your first example, its value is the original value of the variable being incremented.
So even though m1 is incremented before array idexing, the value of
temp1
is assigned at positionm1 - 1
.表达式(或子表达式)有两个方面:它的值,
及其副作用。
i++
的值就是i
的值;这++ i
的值是值i + 1
,转换为i
的类型。这是表达式中使用的值。两者的副作用是
增加变量
i
。这可能发生在前面的序列点和下一个序列点之前。假设
i
是一个全局变量变量,并且您编写如下内容:
标准没有说明
i
的值是否在f()
中出现或
g()
是增量之前或之后。在这两种情况下。标准所说的只是增量的效果将
发生在完整表达式开始之后(但也许作为
完整表达中的第一件事)和结束之前。 (还有那个
它们不会与函数调用交错,因此 if
f()
读取
i
两次,保证看到相同的值。)There are two aspects to an expression (or sub-expression): its value,
and its side effects. The value of
i ++
is the value ofi
; thevalue of
++ i
is the valuei + 1
, converted to the type ofi
.This is the value used in the expression. The side effects of both is
to increment the variable
i
. This may occur at any time after thepreceding sequence point and before the next. Supposing
i
is a globalvariable, and you write something like:
The standard says nothing about whether the value of
i
seen inf()
or
g()
is that before the incrementation, or after. In neither case.All the standard says is that the effects of the incrementation will
take place after the start of the full expression (but perhaps as the
first thing in the full expression) and before the end of it. (And that
they won't be interleaved with a function call, so that if
f()
reads
i
twice, it is guaranteed to see the same value.)不幸的是,在您发布的那两个代码片段中,没有保证评估的顺序。如果你的表达不恰当,或多或少就会发生什么事情。
首先从 a++ 和 ++a 之间的区别开始:
列表
编译器可以决定在表达式中的任何点执行 ++ 的 。因此,如果“b”实际上是涉及 a 的表达式,则可以在不同的编译器上得到不同的结果。以下两者都是有效的:
或 this
位置,如果 'b' 碰巧涉及 a,这两种实现将产生不同的结果。两者都有效。
Unfortunately, in those 2 code snippets you've posted there, there's no guaranteed order of evaluation. If your expressions are inappropriate, more or less anything could happen.
To start with the difference between a++ and ++a:
with
the compiler can decide to do the ++ at any point within the expression. Thus if 'b' is actually an expression involving a, you can get different results on different compilers. Both of the following would be valid:
or this
if 'b' should happen to involve a, those 2 implementations would produce different results. Both are valid.
它的工作原理与您所描述的完全相反:
offSpring1[m1++] = temp1
与offSpring[m1] = temp1; m1 = m1 + 1;
OffSpring1[++m1] = temp1
与m1 = m1 + 1; OffSpring1[m1] = temp1;
在计算表达式之前前缀表示法递增
计算表达式后后缀表示法递增
It works precisely the opposite of what you described:
offSpring1[m1++] = temp1
is the same asoffSpring[m1] = temp1; m1 = m1 + 1;
OffSpring1[++m1] = temp1
is the same asm1 = m1 + 1; OffSpring1[m1] = temp1;
Prefix notation increments before evaluating the expression
Postfix notation increments after evaluating the expression
第一个的描述是第二个的正确描述。第一个的正确描述非常相似,您只需要在其他步骤之前添加“复制 m1 的当前值”步骤即可。
但是,如果
m1
具有原始类型,那么这里确实明显缺乏序列点。 C++03 和 C++11 之间的规则略有变化。如果
m1
具有用户定义的类型,则涉及影响排序的函数调用。此代码
执行以下操作(如果
m1
是基本类型):此代码
完全相同,只是
lhs
使用new_m1
绑定,而不是使用new_m1
old_m1
。无论哪种情况,都未指定
lhs
是写入m1
之前还是之后。如果
m1
不是原始类型,它看起来更像:vs
在这两种情况下,对
m1
的更改肯定是在写入lhs.
The description of the first is the correct description for the second. The correct description of the first is very similar, you just need a "copy current value of m1" step added before the others.
But you do have a distinct lack of sequence points here, if
m1
has a primitive type. The rules change somewhat between C++03 and C++11.If
m1
has a user-defined type, then there are function calls involved which influence sequencing.This code
performs the following (if
m1
is a primitive type):This code
is exactly the same except that
lhs
is bound usingnew_m1
instead ofold_m1
.In either case, it is unspecified whether
lhs
is written to before or afterm1
.If
m1
is not a primitive type, it looks more like:vs
In both these cases, the change to
m1
is definitely made before the write tolhs
.