什么是'-->' C/C++ 中的运算符?

发布于 2024-08-09 10:51:08 字数 503 浏览 5 评论 0原文

阅读C++/STL 的隐藏功能和黑暗角落后在 comp.lang.c++.moderated 上,我完全惊讶于以下代码片段在 Visual Studio 2008 和 G++ 4.4 中都能编译并运行。我认为这也是有效的 C,因为它也适用于 GCC。

代码如下:

#include <stdio.h>
int main()
{
    int x = 10;
    while (x --> 0) // x goes to 0
    {
        printf("%d ", x);
    }
}

输出:

9 8 7 6 5 4 3 2 1 0

标准中在哪里定义了它,它来自哪里?

After reading Hidden Features and Dark Corners of C++/STL on comp.lang.c++.moderated, I was completely surprised that the following snippet compiled and worked in both Visual Studio 2008 and G++ 4.4. I would assume this is also valid C since it works in GCC as well.

Here's the code:

#include <stdio.h>
int main()
{
    int x = 10;
    while (x --> 0) // x goes to 0
    {
        printf("%d ", x);
    }
}

Output:

9 8 7 6 5 4 3 2 1 0

Where is this defined in the standard, and where has it come from?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(26

小耗子 2024-08-16 10:51:08

--> 不是运算符。它实际上是两个独立的运算符,-->

条件中的代码递减 x,同时返回 x 的原始(未递减)值,然后使用以下命令将原始值与 0 进行比较> 运算符。

为了更好地理解,该语句可以写成如下:

while( (x--) > 0 )

--> is not an operator. It is in fact two separate operators, -- and >.

The code in the condition decrements x, while returning x's original (not decremented) value, and then compares the original value with 0 using the > operator.

To better understand, the statement could be written as follows:

while( (x--) > 0 )
愛上了 2024-08-16 10:51:08

或者对于完全不同的东西...x 滑动到0

while (x --\
            \
             \
              \
               > 0)
     printf("%d ", x);

不是那么数学化,但是...每张图片都描绘了一千个单词...

Or for something completely different... x slides to 0.

while (x --\
            \
             \
              \
               > 0)
     printf("%d ", x);

Not so mathematical, but... every picture paints a thousand words...

瑶笙 2024-08-16 10:51:08

这是一个非常复杂的运算符,因此甚至 ISO/IEC JTC1(联合技术委员会 1) 也放置了其描述在 C++ 标准的两个不同部分中。

开玩笑吧,它们是两个不同的运算符:--> 分别在 C++03 标准的 §5.2.6/2 和 §5.9 中描述。

That's a very complicated operator, so even ISO/IEC JTC1 (Joint Technical Committee 1) placed its description in two different parts of the C++ Standard.

Joking aside, they are two different operators: -- and > described respectively in §5.2.6/2 and §5.9 of the C++03 Standard.

夕色琉璃 2024-08-16 10:51:08

在 C++ 中,x 可以在相反方向更快地变为零:

int x = 10;

while( 0 <---- x )
{
   printf("%d ", x);
}

8 6 4 2

您可以用箭头控制速度!

int x = 100;

while( 0 <-------------------- x )
{
   printf("%d ", x);
}

90 80 70 60 50 40 30 20 10

x can go to zero even faster in the opposite direction in C++:

int x = 10;

while( 0 <---- x )
{
   printf("%d ", x);
}

8 6 4 2

You can control speed with an arrow!

int x = 100;

while( 0 <-------------------- x )
{
   printf("%d ", x);
}

90 80 70 60 50 40 30 20 10

旧夏天 2024-08-16 10:51:08

它相当于

while (x-- > 0)

x--(后递减)相当于x = x-1(但返回x的原始值),所以代码转换为:

while(x > 0) {
    x = x-1;
    // logic
}
x--;   // The post decrement done when x <= 0

It's equivalent to

while (x-- > 0)

x-- (post decrement) is equivalent to x = x-1 (but returning the original value of x), so the code transforms to:

while(x > 0) {
    x = x-1;
    // logic
}
x--;   // The post decrement done when x <= 0
苍暮颜 2024-08-16 10:51:08

只是

#include <stdio.h>

int main(void) {
    int x = 10;
    while (x-- > 0) { // x goes to 0
        printf("%d ", x);
    }
    return 0;
}

空格让事情看起来很有趣,-- 递减和> 比较。

It's

#include <stdio.h>

int main(void) {
    int x = 10;
    while (x-- > 0) { // x goes to 0
        printf("%d ", x);
    }
    return 0;
}

Just the space makes the things look funny, -- decrements and > compares.

尽揽少女心 2024-08-16 10:51:08

--> 的使用具有历史相关性。在 x86 架构上,递减曾经(并且在某些情况下仍然如此)比递增更快。使用 --> 表明 x 将变为 0,并且对具有数学背景的人很有吸引力。

The usage of --> has historical relevance. Decrementing was (and still is in some cases), faster than incrementing on the x86 architecture. Using --> suggests that x is going to 0, and appeals to those with mathematical backgrounds.

眼中杀气 2024-08-16 10:51:08

非常极客,但我将使用这个:

#define as ;while

int main(int argc, char* argv[])
{
    int n = atoi(argv[1]);
    do printf("n is %d\n", n) as ( n --> 0);
    return 0;
}

Utterly geek, but I will be using this:

#define as ;while

int main(int argc, char* argv[])
{
    int n = atoi(argv[1]);
    do printf("n is %d\n", n) as ( n --> 0);
    return 0;
}
是你 2024-08-16 10:51:08

我读过的一本书(我不记得是哪本书了)指出:编译器尝试使用左右规则将表达式解析为最大的标记

在这种情况下,表达式:

x-->0

解析为最大标记:

token 1: x
token 2: --
token 3: >
token 4: 0
conclude: x-- > 0

相同的规则适用于此表达式:

a-----b

解析后:

token 1: a
token 2: --
token 3: --
token 4: -
token 5: b
conclude: (a--)-- - b

One book I read (I don't remember correctly which book) stated: Compilers try to parse expressions to the biggest token by using the left right rule.

In this case, the expression:

x-->0

Parses to biggest tokens:

token 1: x
token 2: --
token 3: >
token 4: 0
conclude: x-- > 0

The same rule applies to this expression:

a-----b

After parse:

token 1: a
token 2: --
token 3: --
token 4: -
token 5: b
conclude: (a--)-- - b
夏九 2024-08-16 10:51:08

这与非负数的 while(x--) 完全相同。

This is exactly the same as while(x--) for non-negative numbers.

苏大泽ㄣ 2024-08-16 10:51:08

不管怎样,我们现在有了一个“gos to”操作符。 “-->”作为方向很容易被记住,而“当x变为零”的意思是直线。

此外,在某些平台上,它比 “for (x = 10; x > 0; x --)” 更高效。

Anyway, we have a "goes to" operator now. "-->" is easy to be remembered as a direction, and "while x goes to zero" is meaning-straight.

Furthermore, it is a little more efficient than "for (x = 10; x > 0; x --)" on some platforms.

清风无影 2024-08-16 10:51:08

该代码首先比较 x 和 0,然后递减 x。 (在第一个答案中也说过:您要对 x 进行后递减,然后使用 > 运算符比较 x 和 0。)请参阅此代码的输出:

9 8 7 6 5 4 3 2 1 0

我们现在首先比较,然后递减在输出中看到 0。

如果我们想先递减然后比较,请使用以下代码:

#include <stdio.h>
int main(void)
{
    int x = 10;

    while( --x> 0 ) // x goes to 0
    {
        printf("%d ", x);
    }
    return 0;
}

输出为:

9 8 7 6 5 4 3 2 1

This code first compares x and 0 and then decrements x. (Also said in the first answer: You're post-decrementing x and then comparing x and 0 with the > operator.) See the output of this code:

9 8 7 6 5 4 3 2 1 0

We now first compare and then decrement by seeing 0 in the output.

If we want to first decrement and then compare, use this code:

#include <stdio.h>
int main(void)
{
    int x = 10;

    while( --x> 0 ) // x goes to 0
    {
        printf("%d ", x);
    }
    return 0;
}

That output is:

9 8 7 6 5 4 3 2 1
夏天碎花小短裙 2024-08-16 10:51:08

当我运行此代码时,我的编译器将打印出 9876543210。

#include <iostream>
int main()
{
    int x = 10;

    while( x --> 0 ) // x goes to 0
    {
        std::cout << x;
    }
}

正如预期的那样。 while( x-- > 0 ) 实际上意味着 while( x > 0)x-- 后递减 x

while( x > 0 ) 
{
    x--;
    std::cout << x;
}

是同一件事的不同写法。

很高兴原来的看起来像“当 x 变为 0”。

My compiler will print out 9876543210 when I run this code.

#include <iostream>
int main()
{
    int x = 10;

    while( x --> 0 ) // x goes to 0
    {
        std::cout << x;
    }
}

As expected. The while( x-- > 0 ) actually means while( x > 0). The x-- post decrements x.

while( x > 0 ) 
{
    x--;
    std::cout << x;
}

is a different way of writing the same thing.

It is nice that the original looks like "while x goes to 0" though.

爱情眠于流年 2024-08-16 10:51:08

--> 之间缺少空格。 x 是后递减的,即在检查条件 x>0 ? 后递减。

There is a space missing between -- and >. x is post decremented, that is, decremented after checking the condition x>0 ?.

盛夏尉蓝 2024-08-16 10:51:08

--减量 运算符,>大于 运算符。

这两个运算符作为一个运算符应用,如 -->

-- is the decrement operator and > is the greater-than operator.

The two operators are applied as a single one like -->.

空城之時有危險 2024-08-16 10:51:08

它是两个运算符的组合。第一个 -- 用于递减值,> 用于检查值是否大于右侧操作数。

#include<stdio.h>

int main()
{
    int x = 10;

    while (x-- > 0)
        printf("%d ",x);

    return 0;
}

输出将是:

9 8 7 6 5 4 3 2 1 0            

It's a combination of two operators. First -- is for decrementing the value, and > is for checking whether the value is greater than the right-hand operand.

#include<stdio.h>

int main()
{
    int x = 10;

    while (x-- > 0)
        printf("%d ",x);

    return 0;
}

The output will be:

9 8 7 6 5 4 3 2 1 0            
只为守护你 2024-08-16 10:51:08

C 和 C++ 遵循“最大咀嚼”规则。同样的方式将 a---b 翻译为 (a--) - b,在您的情况下 x-->0 会翻译到(x--)>0

该规则本质上说的是,从左到右,表达式是通过采用将形成有效标记的最大字符来形成的。

C and C++ obey the "maximal munch" rule. The same way a---b is translated to (a--) - b, in your case x-->0 translates to (x--)>0.

What the rule says essentially is that going left to right, expressions are formed by taking the maximum of characters which will form a valid token.

如日中天 2024-08-16 10:51:08

实际上,x 是后递减的,并且正在检查该条件。这不是 -->,而是 (x--) > 0

注意:x 的值在检查条件后会更改,因为它是后递减的。一些类似的情况也可能发生,例如:

-->    x-->0
++>    x++>0
-->=   x-->=0
++>=   x++>=0

Actually, x is post-decrementing and with that condition is being checked. It's not -->, it's (x--) > 0

Note: value of x is changed after the condition is checked, because it post-decrementing. Some similar cases can also occur, for example:

-->    x-->0
++>    x++>0
-->=   x-->=0
++>=   x++>=0
沧桑㈠ 2024-08-16 10:51:08

您可以使用穿甲箭头运算符来代替常规箭头运算符(-->):--x> (注意箭头尖端上的那些锋利的倒刺)。它为穿甲增加 +1,因此它比常规箭头操作员更快地完成循环 1 迭代。自己尝试一下:

int x = 10;
while( --x> 0 )
    printf("%d ", x);

Instead of regular arrow operator (-->) you can use armor-piercing arrow operator: --x> (note those sharp barbs on the arrow tip). It adds +1 to armor piercing, so it finishes the loop 1 iteration faster than regular arrow operator. Try it yourself:

int x = 10;
while( --x> 0 )
    printf("%d ", x);
以酷 2024-08-16 10:51:08

对原始问题的简单回答是,以下代码执行相同的操作(尽管我并不是说您应该这样做):

#include <stdio.h>

int main() {
    int x = 10;
    while (x > 0) {
        printf("%d ", x);
        x = x - 1;
    }
}

x-- 只是上面的简写,而 < code>> 只是一个普通的大于运算符

The simple answer to the original question is that the following code does the same thing (though I am not saying you should do it like this):

#include <stdio.h>

int main() {
    int x = 10;
    while (x > 0) {
        printf("%d ", x);
        x = x - 1;
    }
}

The x-- is just shorthand for the above, and > is just a normal greater-than operator.

南城旧梦 2024-08-16 10:51:08

(x --> 0) 表示 (x-- > 0)

  1. 您可以使用(x -->)
    输出:9 8 7 6 5 4 3 2 1 0
  1. 你可以使用 (-- x > 0) 意思是 (--x > 0)
    输出:9 8 7 6 5 4 3 2 1
  1. 您可以使用
(--\
    \
     x > 0)

Output: 9 8 7 6 5 4 3 2 1

  1. 您可以使用
(\
  \
   x --> 0)

Output: 9 8 7 6 5 4 3 2 1 0

  1. 您可以使用
(\
  \
   x --> 0
          \
           \
            )

Output : 9 8 7 6 5 4 3 2 1 0

  1. 你也可以使用
(
 x 
  --> 
      0
       )

Output: 9 8 7 6 5 4 3 2 1 0

同样,你可以尝试很多方法来执行这个命令成功地。

(x --> 0) means (x-- > 0).

  1. You can use (x -->)
    Output: 9 8 7 6 5 4 3 2 1 0
  1. You can use (-- x > 0) It's mean (--x > 0)
    Output: 9 8 7 6 5 4 3 2 1
  1. You can use
(--\
    \
     x > 0)

Output: 9 8 7 6 5 4 3 2 1

  1. You can use
(\
  \
   x --> 0)

Output: 9 8 7 6 5 4 3 2 1 0

  1. You can use
(\
  \
   x --> 0
          \
           \
            )

Output: 9 8 7 6 5 4 3 2 1 0

  1. You can use also
(
 x 
  --> 
      0
       )

Output: 9 8 7 6 5 4 3 2 1 0

Likewise, you can try lot of methods to execute this command successfully.

扛起拖把扫天下 2024-08-16 10:51:08

常规方式我们在 while 循环括号“()”中定义条件,在大括号“{}”内定义终止条件,但是这个 -- & ; > 是一种一次性定义所有内容的方式。
例如:

int abc(){
    int a = 5
    while((a--) > 0){ // Decrement and comparison both at once
        // Code
    }
}

它说,递减 a 并运行循环,直到 a 大于 0

其他方式应该是这样的:

int abc() {
    int a = 5;
    while(a > 0) {
        a = a -1 // Decrement inside loop
        // Code
    }
}

两者方式,我们做同样的事情,实现同样的目标。

Conventional way we define condition in while loop parenthesis"()" and terminating condition inside the braces"{}", but this -- & > is a way one defines all at once.
For example:

int abc(){
    int a = 5
    while((a--) > 0){ // Decrement and comparison both at once
        // Code
    }
}

It says, decrement a and run the loop till the time a is greater than 0

Other way it should have been like:

int abc() {
    int a = 5;
    while(a > 0) {
        a = a -1 // Decrement inside loop
        // Code
    }
}

Both ways, we do the same thing and achieve the same goals.

z祗昰~ 2024-08-16 10:51:08

--> 不是运算符,它是 --(后自减)和 >(大于比较)的并置。

该循环看起来更熟悉:

#include <stdio.h>
int main() {
    int x = 10;
    while (x-- > 0) { // x goes to 0
        printf("%d ", x);
    }
}

此循环是一个经典的习惯用法,用于枚举 10 (排除的上限)和 0 包含的下限之间的值,对于迭代非常有用数组中从最后一个到第一个的元素。

初始值10是迭代总数(例如数组的长度),加上循环内使用的第一个值。 0 是循环内 x 的最后一个值,因此注释 x 变为 0

请注意,循环完成后x 的值为-1

另请注意,如果 x 具有无符号类型(例如 size_t),则此循环将以相同方式运行,即与 (i = length-1; i >= 0; i--) 的朴素替代方案相比,这是一个强大的优势。

出于这个原因,我实际上很喜欢这种令人惊讶的语法:while (x --> 0)。我发现这个习语引人注目且优雅,就像 for (;;) 与: while (1) (看起来与 while (l) 令人困惑) )。它也适用于语法受 C 启发的其他语言:C++、Objective-C、java、javascript、C# 等等。

--> is not an operator, it is the juxtaposition of -- (post-decrement) and > (greater than comparison).

The loop will look more familiar as:

#include <stdio.h>
int main() {
    int x = 10;
    while (x-- > 0) { // x goes to 0
        printf("%d ", x);
    }
}

This loop is a classic idiom to enumerate values between 10 (the excluded upper bound) and 0 the included lower bound, useful to iterate over the elements of an array from the last to the first.

The initial value 10 is the total number of iterations (for example the length of the array), and one plus the first value used inside the loop. The 0 is the last value of x inside the loop, hence the comment x goes to 0.

Note that the value of x after the loop completes is -1.

Note also that this loop will operate the same way if x has an unsigned type such as size_t, which is a strong advantage over the naive alternative for (i = length-1; i >= 0; i--).

For this reason, I am actually a fan of this surprising syntax: while (x --> 0). I find this idiom eye-catching and elegant, just like for (;;) vs: while (1) (which looks confusingly similar to while (l)). It also works in other languages whose syntax is inspired by C: C++, Objective-C, java, javascript, C# to name a few.

感情旳空白 2024-08-16 10:51:08

这里 -- 是一元后减运算符。

 while (x-- > 0) // x goes to 0
 {
     printf("%d ", x);
 }
  • 一开始,条件将评估为
    (x > 0) // 10 > 0
  • 现在因为条件为真,它将进​​入循环并减少值
    x-- // x = 9
  • 这就是为什么第一个打印的值为 9
  • 等等。在最后一个循环中x=1,所以条件为真。根据一元运算符,该值在打印时更改为 x = 0
  • 现在,x = 0,它将条件 (x > 0 ) 计算为 false,并且 while 循环退出。

Here -- is the unary post decrement operator.

 while (x-- > 0) // x goes to 0
 {
     printf("%d ", x);
 }
  • In the beginning, the condition will evaluate as
    (x > 0) // 10 > 0
  • Now because the condition is true, it will go into the loop with a decremented value
    x-- // x = 9
  • That's why the first printed value is 9
  • And so on. In the last loop x=1, so the condition is true. As per the unary operator, the value changed to x = 0 at the time of print.
  • Now, x = 0, which evaluates the condition (x > 0 ) as false and the while loop exits.
咽泪装欢 2024-08-16 10:51:08

这个 --> 根本不是一个运算符。我们有一个类似于 -> 的运算符,但不像 --> 那样。这只是 while(x-- >0) 的错误解释,它只是意味着 x 具有后递减运算符,并且此循环将运行直到它大于

编写此代码的另一种简单方法是 while(x--)while循环只要遇到错误条件就会停止,这里只有一种情况,即0。因此,当 x 值减至时,它将停止。

This --> is not an operator at all. We have an operator like ->, but not like -->. It is just a wrong interpretation of while(x-- >0) which simply means x has the post decrement operator and this loop will run till it is greater than zero.

Another simple way of writing this code would be while(x--). The while loop will stop whenever it gets a false condition and here there is only one case, i.e., 0. So it will stop when the x value is decremented to zero.

财迷小姐 2024-08-16 10:51:08

这就是你的意思。

while((x--) > 0)

我们小时候就听过,

别停下来,放手(Road Go)

?????????????????????????????????????????????????????????????????????????????????????????????????????????

停下来,别放手。

发生同样的情况,空格会造成混乱。 :D

That's what you mean.

while((x--) > 0)

We heard in childhood,

Stop don't, Let Go (روکو مت، جانے دو)

Where a Comma makes confusion

Stop, don't let go. (روکو، مت جانے دو)

Same Happens in Programming now, a SPACE makes confusion. :D

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