前/后增量说明

发布于 2024-12-27 19:46:31 字数 893 浏览 1 评论 0原文

请对我宽容点,不要开枪打我,因为我还是新手。

当我运行这段代码时,我完全困惑了,无法弄清楚为什么:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

も让我眼熟你 2025-01-03 19:46:31

一个简单的规则是,您不应在任何给定语句中多次增加同一位置。所以你不应该编写 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 of y (assuming an int 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!

流绪微梦 2025-01-03 19:46:31

当按照规则运算*要算在+之前,++要算在*之前时,就是这样。

 a*b++ + c // first b++ (returns **old** b), than a*b, than ...+c

但是当你有 a++ * a-- 时,没有人可以知道,a++ 或 a-- 这两个操作数中的哪一个将首先被求值。根据 ANSII 标准,即使使用相同的翻译器,每次的结果也是不可预测的。

引用 C++ ANSII 标准:

除非另有说明,各个操作数的求值顺序
各个表达式的运算符和子表达式,以及顺序
哪些副作用发生,尚未明确。之间的前
标量对象应存储当前和下一个序列点
通过表达式的求值最多修改一次值。毛皮-
此外,仅应访问先验值来确定
要存储的值。应满足本款要求
对于完整表达式的子表达式的每个允许的排序
锡安;否则行为是未定义的。 [示例:

      i = v[i++];      // the behavior is undefined
      i = 7, i++, i++; // `i' becomes 9

      i = ++i + 1;     // the behavior is undefined 
      i = i + 1;       // the value of 'i' is incremented

序列点:

  • 在完整表达式求值结束时(完整表达式是表达式语句,或不是任何较大表达式中的子表达式的任何其他表达式);
  • 在 ||、&&、?: 和逗号运算符处;
  • 并在函数调用时(在评估所有参数之后,并且
    就在实际通话之前)。

所以,||是一个序列点,但是 <<不是。

When according to the rules operation * is to be counted before +, and ++ before *, it will be so.

 a*b++ + c // first b++ (returns **old** b), than a*b, than ...+c

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:

      i = v[i++];      // the behavior is undefined
      i = 7, i++, i++; // `i' becomes 9

      i = ++i + 1;     // the behavior is undefined 
      i = i + 1;       // the value of 'i' is incremented

Sequence points:

  • at the end of the evaluation of a full expression (a full expression is an expression statement, or any other expression which is not a subexpression within any larger expression);
  • at the ||, &&, ?:, and comma operators;
  • and at a function call (after the evaluation of all the arguments, and
    just before the actual call).

So, || is a sequence point, but << is not.

只是我以为 2025-01-03 19:46:31

第一个代码的多行版本应该是:

  y = 9;
  cout << "y-- = " << y-- << "\n";
  cout << "y++ = " << y++ << "\n"
  cout << "--y = " << --y << "\n"
  cout << "++y = " << ++y << "\n"
  cout << "y = " << y << "\n";

Mulitiline version of first code should be:

  y = 9;
  cout << "y-- = " << y-- << "\n";
  cout << "y++ = " << y++ << "\n"
  cout << "--y = " << --y << "\n"
  cout << "++y = " << ++y << "\n"
  cout << "y = " << y << "\n";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文