[英]What is the "-->" operator in C++?
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.在
comp.lang.c++.moderated
上阅读C++/STL 的隐藏功能和暗角后,我完全惊讶于以下代码片段在 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: Output:
9 8 7 6 5 4 3 2 1 0
I'd assume this is C, since it works in GCC as well.我假设这是 C,因为它也适用于 GCC。 Where is this defined in the standard, and where has it come from?
这在标准中是哪里定义的,它是从哪里来的?
-->
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 )
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...不是那么数学,但是...每张图片都描绘了一千个单词...
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 中描述。
x
can go to zero even faster in the opposite direction in C++: x
可以在 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
;) ;)
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
因此,代码转换为:
while(x > 0) {
x = x-1;
// logic
}
x--; // The post decrement done when x <= 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.只是空间让事情看起来很有趣,
--
递减和>
比较。
The usage of -->
has historical relevance. -->
的用法具有历史意义。 Decrementing was (and still is in some cases), faster than incrementing on the x86 architecture.在 x86 架构上,递减曾经(并且在某些情况下仍然如此)比递增快。 Using
-->
suggests that x
is going to 0
, and appeals to those with mathematical backgrounds.使用
-->
表明x
将变为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;
}
while( x-- > 0 )
是如何解析的。
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
这与
while (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. "-->"
作为一个方向很容易被记住,而“当x变为零时”是直截了当的意思。
Furthermore, it is a little more efficient than "for (x = 10; x > 0; x --)"
on some platforms.此外,在某些平台上,它比
"for (x = 10; x > 0; x --)"
更有效。
This code first compares x and 0 and then decrements x.此代码首先比较 x 和 0,然后递减 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: (在第一个答案中也说过:您正在递减 x ,然后将 x 和 0 与
>
运算符进行比较。)请参阅此代码的输出:
9 8 7 6 5 4 3 2 1 0
We now first compare and then decrement by seeing 0 in the output.我们现在首先比较,然后通过在输出中看到 0 来递减。
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
My compiler will print out 9876543210 when I run this code.当我运行此代码时,我的编译器将打印出 9876543210。
#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)
. while( x-- > 0 )
实际上意味着while( x > 0)
。 The x--
post decrements x
. x--
后减量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.很高兴原始看起来像“虽然 x 变为 0”。
There is a space missing between --
and >
. --
和>
之间缺少一个空格。 x
is post decremented, that is, decremented after checking the condition x>0 ?
x
后递减,即检查条件x>0 ?
. .
--
is the decrement operator and >
is the greater-than operator. --
是递减运算符, >
是大于运算符。
The two operators are applied as a single one like -->
.这两个运算符作为一个单独的运算符应用
-->
。
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
C and C++ obey the "maximal munch" rule. C 和 C++ 遵循“最大咀嚼”规则。 The same way
a---b
is translated to (a--) - b
, in your case x-->0
translates to (x--)>0
.与
a---b
转换为(a--) - b
的方式相同,在您的情况下x-->0
转换为(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.该规则本质上说的是,从左到右,表达式是通过获取将形成有效标记的最大字符来形成的。
Actually, x
is post-decrementing and with that condition is being checked.实际上,
x
是后递减的,并且正在检查该条件。 It's not -->
, it's (x--) > 0
不是
-->
,而是(x--) > 0
Note: value of x
is changed after the condition is checked, because it post-decrementing.注意:
x
的值在条件检查后会更改,因为它是在递减后。 Some similar cases can also occur, for example:一些类似的情况也可能发生,例如:
--> x-->0
++> x++>0
-->= x-->=0
++>= x++>=0
Why all the complication?为什么所有的并发症?
The simple answer to the original question is just:原始问题的简单答案是:
#include <stdio.h>
int main()
{
int x = 10;
while (x > 0)
{
printf("%d ", x);
x = x-1;
}
}
It does the same thing.它做同样的事情。 I am not saying you should do it like this, but it does the same thing and would have answered the question in one post.
我并不是说你应该这样做,但它会做同样的事情,并且会在一篇文章中回答这个问题。
The x--
is just shorthand for the above, and >
is just a normal greater-than operator
. x--
只是上面的简写,而>
只是一个普通的大于operator
。 No big mystery!没有什么大谜团!
There are too many people making simple things complicated nowadays ;)现在有太多人把简单的事情复杂化了;)
char sep = '\n' /1\
; int i = 68 /1 \
; while (i --- 1\
\
/1/1/1 /1\
/1\
/1\
/1\
/1\
/ 1\
/ 1 \
/ 1 \
/ 1 \
/1 /1 \
/1 /1 \
/1 /1 /1/1> 0) std::cout \
<<i<< sep;
For larger numbers, C++20 introduces some more advanced looping features.对于更大的数字,C++20 引入了一些更高级的循环特性。 First to catch
i
we can build an inverse loop-de-loop and deflect it onto the std::ostream
.首先要捕获
i
,我们可以构建一个反向循环解循环并将其转移到std::ostream
上。 However, the speed of i
is implementation-defined, so we can use the new C++20 speed operator <<i<<
to speed it up.但是,
i
的速度是实现定义的,因此我们可以使用新的 C++20 速度运算符<<i<<
来加速它。 We must also catch it by building wall, if we don't, i
leaves the scope and de referencing it causes undefined behavior.我们还必须通过构建墙来捕获它,如果我们不这样做,
i
会离开范围并取消引用它会导致未定义的行为。 To specify the separator, we can use:要指定分隔符,我们可以使用:
std::cout \
sep
and there we have a for loop from 67 to 1.我们有一个从 67 到 1 的 for 循环。
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.我们在 while 循环括号“
()
”中定义条件并在大括号“ {}
”中定义条件的传统方式,但是这--
& >
是一种一次性定义所有条件的方式。 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
它说,递减
a
并运行循环,直到时间a
大于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.两种方式,我们做同样的事情,实现同样的目标。
(x --> 0)
means (x-- > 0)
. (x --> 0)
表示(x-- > 0)
。
(x -->)
(x -->)
Output: 9 8 7 6 5 4 3 2 1 0
(-- x > 0)
It's mean (--x > 0)
(-- x > 0)
这意味着(--x > 0)
Output: 9 8 7 6 5 4 3 2 1
(--\
\
x > 0)
Output: 9 8 7 6 5 4 3 2 1
(\
\
x --> 0)
Output: 9 8 7 6 5 4 3 2 1 0
(\
\
x --> 0
\
\
)
Output: 9 8 7 6 5 4 3 2 1 0
(
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.同样,您可以尝试很多方法来成功执行此命令。
Instead of regular arrow operator (-->) you can use armor-piercing arrow operator: --x> (note those sharp barbs on the arrow tip).您可以使用穿甲箭头操作符来代替常规的箭头操作符 (-->):--x> (注意箭头尖端的那些锋利的倒钩)。 It adds +1 to armor piercing, so it finishes the loop 1 iteration faster than regular arrow operator.
它为穿甲增加了 +1,因此它比常规箭头运算符更快地完成循环 1 迭代。 Try it yourself:
自己试试:
int x = 10;
while( --x> 0 )
printf("%d ", 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 .这只是对
while(x-- >0)
的错误解释,这仅意味着 x 具有后减量运算符,并且此循环将运行直到它大于零。
Another simple way of writing this code would be while(x--)
.编写此代码的另一种简单方法是
while(x--)
。 The while loop will stop whenever it gets a false condition and here there is only one case, ie, 0
.当while循环遇到错误条件时将停止,这里只有一种情况,即
0
。 So it will stop when the x value is decremented to zero .因此,当 x 值减为零时,它将停止。
Here --
is the unary post decrement operator.这里
--
是一元后减运算符。
while (x-- > 0) // x goes to 0
{
printf("%d ", x);
}
(x > 0) // 10 > 0
(x > 0) // 10 > 0
x-- // x = 9
x-- // x = 9
进入循环x=1
, so the condition is true.x=1
,所以条件为真。 As per the unary operator, the value changed to x = 0
at the time of print.x = 0
。x = 0
, which evaluates the condition (x > 0 )
as false and the while loop exits.x = 0
,它将条件(x > 0 )
评估为 false 并且while循环退出。-->
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.此循环是枚举
10
(排除的上限)和0
包含的下限之间的值的经典习惯用法,对于从最后一个到第一个遍历数组元素很有用。
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.初始值
10
是迭代的总数(例如数组的长度),加上循环内使用的第一个值。 The 0
is the last value of x
inside the loop, hence the comment x goes to 0 . 0
是循环内x
的最后一个值,因此注释x 变为 0 。
Note that the value of x
after the loop completes is -1
.请注意,循环完成后
x
的值为-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--)
.另请注意,如果
x
具有诸如size_t
之类的无符号类型,则此循环将以相同的方式运行,这比for (i = length-1; i >= 0; i--)
的幼稚替代方案具有很大优势。
For this reason, I am actually a fan of this surprising syntax: while (x --> 0)
.出于这个原因,我实际上是这种令人惊讶的语法的粉丝:
while (x --> 0)
。 I find this idiom eye-catching and elegant, just like for (;;)
vs: while (1)
(which looks confusingly similar to while (l)
).我发现这个成语引人注目且优雅,就像
for (;;)
vs: while (1)
(看起来与while (l)
令人困惑地相似)。 It also works in other languages whose syntax is inspired by C: C++, Objective-C, java, javascript, C# to name a few.它也适用于语法受 C 启发的其他语言:C++、Objective-C、java、javascript、C# 等等。
Actually, you can "create" a --> operator just for fun )实际上,您可以“创建”一个 --> 运算符只是为了好玩)
class MyClass {
class Helper
{
MyClass* ptr;
Helper(MyClass* _this): ptr{_this} {}
public:
Helper(const Helper&) = delete;
Helper(Helper&&) = delete;
void operator=(const Helper&) = delete;
void operator=(Helper&&) = delete;
operator MyClass()
{
auto tmp = *ptr;
tmp._value++;
return tmp;
}
friend MyClass;
void operator>(int){std::cout << "Operator -->" << std::endl;}
};
int _value = 0;
public:
MyClass() = default;
MyClass(int value): _value{value} {}
Helper operator--(int)
{
_value--;
return Helper(this);
}
int get() const noexcept
{
return _value;
}
bool operator>(int num) const noexcept
{
return _value > num;
}
};
int main()
{
MyClass obj(5);
obj > 1; //operator >
obj--; //operator --
MyClass b = obj--; //still works
std::cout << b.get() << std::endl; //4
std::cout << obj.get() << std::endl; //3
b --> 5; //operator -->
//But there is at least one problem
auto c = obj--; //auto can deduce a private type :(
}
But as I said it's only for fun ;)但正如我所说,这只是为了好玩;)
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.现在编程中也会发生同样的情况,SPACE 会造成混乱。 :D
:D
Operators are symbols that perform operations on variables and values.运算符是对变量和值执行操作的符号。 For example, + is an operator used for addition, while - is an operator used for subtraction.
例如,+ 是用于加法的运算符,而 - 是用于减法的运算符。
--> it is not an operator actually they are two different operators -- or >. --> 它不是一个运算符,实际上它们是两个不同的运算符 -- 或 >。
Operators in C++ can be classified into 6 types C++中的运算符可分为6种
Arithmetic Operators | +, -, *, /, %
Assignment Operators | =, +=, -=, *=, /=, %=
Relational Operators | ==, !=, >, <, >=, <=
Logical Operators | &&, ||, !
Bitwise Operators | &, |, ^, ~, <<, >>
Other Operators | sizeof, ?:, &, ., ->, <<, >>
Put a space between 2 operators otherwise the compiler take it as a single operator. 在2个运算符之间放置一个空格,否则编译器会将其视为单个运算符。 (z-- > 0)
(z--> 0)
Its actually postfix operation它实际上是后缀操作
while(x-- > 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.