前/后增量说明
请对我宽容点,不要开枪打我,因为我还是新手。
当我运行这段代码时,我完全困惑了,无法弄清楚为什么:
int y = 9;
cout << "++y = " << ++y << "\n--y = " << --y << "\ny++ = " << y++ << "\ny-- = " << y-- << "\n";
cout << "y = " << y << "\n";
我得到以下结果:
y = 9
++y = 9
--y = 9
y++ = 8
y-- = 9
y = 9
而不是这些结果:
y = 9
++y = 10
--y = 9
y++ = 9
y-- = 10
y = 9
我从这段代码中得到:
int y = 9;
cout << "y = " << y << "\n";
cout << "++y = " << ++y << "\n";
cout << "--y = " << --y << "\n";
cout << "y++ = " << y++ << "\n";
cout << "y-- = " << y-- << "\n";
cout << "y = " << y << "\n";
任何人都可以解释 - 用尽可能简单的话 - 第一个代码中发生了什么,以便它以这种方式打印结果?
Please be easy on me and don't shoot me as I'm still newbie.
I'm totally confused and can't for life figure out why when I run this code:
int y = 9;
cout << "++y = " << ++y << "\n--y = " << --y << "\ny++ = " << y++ << "\ny-- = " << y-- << "\n";
cout << "y = " << y << "\n";
I get the following results:
y = 9
++y = 9
--y = 9
y++ = 8
y-- = 9
y = 9
instead of these results:
y = 9
++y = 10
--y = 9
y++ = 9
y-- = 10
y = 9
That I get from this code:
int y = 9;
cout << "y = " << y << "\n";
cout << "++y = " << ++y << "\n";
cout << "--y = " << --y << "\n";
cout << "y++ = " << y++ << "\n";
cout << "y-- = " << y-- << "\n";
cout << "y = " << y << "\n";
Can anyone explain -in simple words as possible- what happens in the first code so that it prints the result that way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个简单的规则是,您不应在任何给定语句中多次增加同一位置。所以你不应该编写
cout << y++ << ++y << endl;
其中包含两个y
增量(假设有int y;
声明)。有关详细信息,请阅读 C++ 标准中的序列点和未定义行为。
有很多相关的问题。调查他们以了解更多!
A simple rule is that you are not expected to increment the same location more than once in any given statement. So you should not code
cout << y++ << ++y << endl;
which contain two increments ofy
(assuming anint y;
declaration).For details, read about sequence points and undefined behavior in the C++ standard.
There are lot of related questions. Look into them for more!
当按照规则运算*要算在+之前,++要算在*之前时,就是这样。
但是当你有 a++ * a-- 时,没有人可以知道,a++ 或 a-- 这两个操作数中的哪一个将首先被求值。根据 ANSII 标准,即使使用相同的翻译器,每次的结果也是不可预测的。
引用 C++ ANSII 标准:
除非另有说明,各个操作数的求值顺序
各个表达式的运算符和子表达式,以及顺序
哪些副作用发生,尚未明确。之间的前
标量对象应存储当前和下一个序列点
通过表达式的求值最多修改一次值。毛皮-
此外,仅应访问先验值来确定
要存储的值。应满足本款要求
对于完整表达式的子表达式的每个允许的排序
锡安;否则行为是未定义的。 [示例:
序列点:
就在实际通话之前)。
所以,||是一个序列点,但是 <<不是。
When according to the rules operation * is to be counted before +, and ++ before *, it will be so.
But when you have a++ * a--, nobody can tell, what of the two operands, a++ or a-- will be evaluated the first. According to ANSII standard, even if you use the same translator, the result is every time unpredictable.
cite from the C++ ANSII standard:
Except where noted, the order of evaluation of operands of individual
operators and subexpressions of individual expressions, and the order
in which side effects take place, is unspecified. Between the previ-
ous and next sequence point a scalar object shall have its stored
value modified at most once by the evaluation of an expression. Fur-
thermore, the prior value shall be accessed only to determine the
value to be stored. The requirements of this paragraph shall be met
for each allowable ordering of the subexpressions of a full expres-
sion; otherwise the behavior is undefined. [Example:
Sequence points:
just before the actual call).
So, || is a sequence point, but << is not.
第一个代码的多行版本应该是:
Mulitiline version of first code should be: