C++ 中的“-->”运算符是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1642028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the "-->" operator in C++?
提问by GManNickG
After reading Hidden Features and Dark Corners of C++/STLon comp.lang.c++.moderated
, I was completely surprised that the following snippet compiled and worked in both Visual Studio 2008 and G++ 4.4.
看完后隐藏的功能和C ++ / STL的暗角上comp.lang.c++.moderated
,我完全意外的是,下面的代码片断编译并在两个Visual Studio 2008和G ++ 4.4的工作。
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
I'd assume this is C, since it works in GCC as well. Where is this defined in the standard, and where has it come from?
我认为这是 C,因为它也适用于 GCC。这在标准中在哪里定义,它来自哪里?
采纳答案by Charles Salvia
-->
is not an operator. It is in fact two separate operators, --
and >
.
-->
不是运营商。它实际上是两个独立的运算符,--
和>
。
The conditional's code decrements x
, while returning x
's original (not decremented) value, and then compares the original value with 0
using the >
operator.
条件的代码递减x
,同时返回x
的原始(未递减)值,然后将原始值与0
使用>
运算符进行比较。
To better understand, the statement could be written as follows:
为了更好地理解,该语句可以写成如下:
while( (x--) > 0 )
回答by unsynchronized
Or for something completely different... x
slides to 0
.
或者对于完全不同的东西......x
滑动到0
.
while (x --\
\
\
\
> 0)
printf("%d ", x);
Not so mathematical, but... every picture paints a thousand words...
不是那么数学,但是……每张图片都描绘了一千个字……
回答by Kirill V. Lyadvinsky
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.
这是一个非常复杂的运算符,因此即使是ISO/IEC JTC1(联合技术委员会 1)也将其描述放在 C++ 标准的两个不同部分中。
Joking aside, they are two different operators: --
and >
described respectively in §5.2.6/2 and §5.9 of the C++03 Standard.
顺便说一句,它们是两个不同的运算符:--
和>
分别在 C++03 标准的 §5.2.6/2 和 §5.9 中描述。
回答by Jay Riggs
It's equivalent to
它相当于
while (x-- > 0)
x--
(post decrement) is equivalent to x = x-1
so, the code transforms to:
x--
(post decrement) 等价于x = x-1
so,代码转换为:
while(x > 0) {
x = x-1;
// logic
}
x--; // The post decrement done when x <= 0
回答by doc
x
can go to zero even faster in the opposite direction:
x
可以在相反方向更快地归零:
int x = 10;
while( 0 <---- x )
{
printf("%d ", x);
}
8 6 4 2
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
90 80 70 60 50 40 30 20 10
;)
;)
回答by RageZ
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 make the things look funny, --
decrements and >
compares.
只是空间让事情看起来很有趣,--
减少和>
比较。
回答by Matt Joiner
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.
的使用-->
具有历史意义。在 x86 架构上,递减(在某些情况下仍然如此)比递增更快。使用-->
表明这x
将会0
,并吸引那些具有数学背景的人。
回答by Grumdrig
while( x-- > 0 )
is how that's parsed.
这是如何解析的。
回答by Escualo
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;
}
回答by NguyenDat
One book I read (I don't remember correctly which book) stated: Compilers try to parse expressions to the biggest tokenby 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 ruleapplies to this expression:
同样的规则适用于这个表达式:
a-----b
After parse:
解析后:
token 1: a
token 2: --
token 3: --
token 4: -
token 5: b
conclude: (a--)-- - b
I hope this helps to understand the complicated expression ^^
我希望这有助于理解复杂的表达^^