使用此表达式的C ++中的魔术x = y - x +(y = x) [英] Magic in C++ with this expression x = y - x + (y = x)

查看:177
本文介绍了使用此表达式的C ++中的魔术x = y - x +(y = x)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,您可以使用以下表达式轻松交换2个变量的值:

  x,y = y ,x 

另一方面,如果要在C ++中交换2个变量的值,你通常使用一个临时变量来存储你的变量的值,类似这样:

  int var1 = 100; 
int var2 = 200;

int temp = var1;
var1 = var2;
var2 = temp;

这很容易,但是你必须写很多代码。



我一直在关注C ++课程的教授发现了一种新的紧凑方式,以一种神奇的方式交换2个变量的值:

  int x = 200; 
int y = 100;

x = y - x +(y = x);

看起来不可思议,但它与编译器一起使用和我的 Apple LLVM版本6.0(clang-600.0.56)



似乎解释表达式的方式如下:



  1. (y = x)//返回x的值

  2. x + x = 0

  3. x = y + 0

  4. x = y

  5. y = x / / finally,y收到x的初始值


变量在一个循环中,它似乎也工作:

  for(int i = -10; i <= 10; i ++ ){
for(int j = 10; j> = -10; j--){
int x = i,y = j;
x = y-x +(y = x);

std :: cout<< x =<< x < \\\
y =<< y < '\\\
';
}
}



我们已经看到交换完成了,编译器给我这个警告:


main.cpp:28:22:warning:unsequenced modify and access to'y'
[-Wunsequenced]


如果有警告,我想这不是一个新的标准方法交换2个变量的值,



为什么这个方法在C ++中不是标准化的,为什么它能正常工作?

解决方案

这是未定义的行为,它不能保证工作。未指定表达式中的参数的求值顺序。因此,允许在执行 y = x 赋值之前或之后访问 y 的值。 / p>

这样指定的原因是为了允许优化的灵活性。


In Python, you can easily exchange the values of 2 variables with an expression like this:

x, y = y, x

In C++, on the other hand, if you want to exchange the values of 2 variables, you usually use a temporary variable to store the value of one of your variables, something like this:

int var1 = 100;
int var2 = 200;

int temp = var1;
var1 = var2;
var2 = temp;

This is easy, but you have to write a lot of code.

A professor that I have been following for his lectures on C++ has discovered a new compact way of exchange the values of 2 variables in a magic way:

int x = 200;
int y = 100;

x = y - x + (y = x);

It seems incredible, but it works both with the compiler he's using and mine Apple LLVM version 6.0 (clang-600.0.56).

It seems that the way that expression is interpreted is the following:

  1. (y = x) // returns the value of x
  2. -x + x = 0
  3. x = y + 0
  4. x = y
  5. y = x // finally, y receives the initial value of x

If I try to exchange the values of some variables in a loop, it seems also to work:

for (int i = -10; i <= 10; i++) {
    for (int j = 10; j >= -10; j--) {
        int x = i, y = j;
        x = y - x + (y = x);

        std::cout << "x = " << x << "\ny = " << y << '\n';
    }
}

We have seen that the exchange is done, but my compiler gives me this warning:

main.cpp:28:22: warning: unsequenced modification and access to 'y' [-Wunsequenced]

If there's a warning, I suppose this is not a new standard way of exchanging the values of 2 variables, but was just an ingenious workaround of this professor.

Why this method is not standardised in C++, and why exactly does it work?

解决方案

This is undefined behavior, and it's not guaranteed to work. The order of evaluation of the parameters in an expression is not specified. So it's allowed to access the value of y for the subtraction before or after it performs the y = x assignment.

The reason it's specified like this is to allow flexibility for optimizations.

这篇关于使用此表达式的C ++中的魔术x = y - x +(y = x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆