有关交换的问题 [英] A question about swap

查看:54
本文介绍了有关交换的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这个问题不会出现两次。我尝试发布一次,

然而,我在谷歌上得到了一个愚蠢的505内部错误。当这个

发生时,帖子会在30分钟后发布,或者不会发布在

全部。


鉴于


void swap(int * x,int * y)

{

int temp;

temp = * x;

* x = * y;

* y = temp;

}


如果让一个临时变量允许任何东西与

本身安全交换?假设x = 10且y = 10.在没有

temp的情况下会发生什么?


Chad

I hope this question doesn''t appear twice. I tried posting it once,
however, I got a stupid 505 internal error on google. When this
happens, the post will either post 30 minutes later or won''t post at
all.

Given

void swap(int *x, int *y)
{
int temp;

temp = *x;
*x = *y;
*y = temp;
}

How does having a temp variable allow for anything to safely swap with
itself? Say x = 10 and y = 10. What would happen in the absence of
temp?

Chad

推荐答案

Chad写道:
Chad wrote:

我希望这个问题不会出现两次。我尝试发布一次,

然而,我在谷歌上得到了一个愚蠢的505内部错误。当这个

发生时,帖子会在30分钟后发布,或者不会发布在

全部。


鉴于


void swap(int * x,int * y)

{

int temp;

temp = * x;

* x = * y;

* y = temp;

}


如果让一个临时变量允许任何东西与

本身安全交换?假设x = 10且y = 10.在没有

temp的情况下会发生什么?
I hope this question doesn''t appear twice. I tried posting it once,
however, I got a stupid 505 internal error on google. When this
happens, the post will either post 30 minutes later or won''t post at
all.

Given

void swap(int *x, int *y)
{
int temp;

temp = *x;
*x = *y;
*y = temp;
}

How does having a temp variable allow for anything to safely swap with
itself? Say x = 10 and y = 10. What would happen in the absence of
temp?



尝试编写没有它的函数并查看你得到了什么。


-

Ian Collins。

Try writing the function without it and see what you get.

--
Ian Collins.


Chad< cd ** ***@gmail.com写:
Chad <cd*****@gmail.comwrites:

我希望这个问题不会出现两次。
I hope this question doesn''t appear twice.



[...]


确实如此。看看我对另一个的回复。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

诺基亚

"我们必须做一点事。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

[...]

It did. See my response to the other one.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


Chad写道:
Chad wrote:

我希望这个问题不会出现两次。我尝试发布一次,

然而,我在谷歌上得到了一个愚蠢的505内部错误。当这个

发生时,帖子会在30分钟后发布,或者不会发布在

全部。


鉴于


void swap(int * x,int * y)

{

int temp;

temp = * x;

* x = * y;

* y = temp;

}


如果让一个临时变量允许任何东西与

本身安全交换?假设x = 10且y = 10.在没有

temp的情况下会发生什么?
I hope this question doesn''t appear twice. I tried posting it once,
however, I got a stupid 505 internal error on google. When this
happens, the post will either post 30 minutes later or won''t post at
all.

Given

void swap(int *x, int *y)
{
int temp;

temp = *x;
*x = *y;
*y = temp;
}

How does having a temp variable allow for anything to safely swap with
itself? Say x = 10 and y = 10. What would happen in the absence of
temp?



好​​吧,x'的价值将被无可挽回地覆盖,不是吗?如果你确实不喜欢临时变量,那么查找所谓的XOR

swap。


< http://en.wikipedia.org/wiki/XOR_swap_algorithm>

Well, x''s value will get irretrievably overwritten won''t it? If you
really dislike the temporary variable, then look-up the so-called "XOR
swap".

<http://en.wikipedia.org/wiki/XOR_swap_algorithm>


这篇关于有关交换的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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