【发布时间】:2013-12-25 23:42:47
【问题描述】:
我的目标是按大小对极坐标形式的向量数组进行排序。我使用冒泡排序实现了这一点,将 PolarVectors 中的所有值一一交换:
- (void) sortVectorsByMagnitude: (PolarVector*)one and:(PolarVector*)two
{
if (one.magnitude < two.magnitude)
{
PolarVector *temp = [[PolarVector alloc] init];
temp.magnitude = one.magnitude;
temp.angle = one.angle;
one.magnitude = two.magnitude;
one.angle = two.angle;
two.magnitude = temp.magnitude;
two.angle = temp.angle;
}
}
这行得通,但看起来很乱而且冗长。我尝试了以下代码但没有成功。据我了解,我正在交换此函数的本地指针,而不是我的主向量指针。
- (void) sortVectorsByMagnitude: (PolarVector*)one and:(PolarVector*)two
{
if (one.magnitude < two.magnitude)
{
PolarVector *temp = [[PolarVector alloc] init];
temp = one;
one = two;
two = temp;
}
}
我尝试如下实现这个解决方案Swapping pointers:
- (void) sortVectorsByMagnitude: (PolarVector*)one and:(PolarVector*)two
{
if (one.magnitude < two.magnitude)
{
PolarVector temp;
temp = *one;
*one = *two;
*two = temp;
}
}
这会产生错误:
PolarVector temp "Interface type cannot be statically allocated"
temp = *one; "Assigning to 'PolarVector *' from incompatible type 'PolarVector'
*two = temp; "Assigning to 'PolarVector' from incompatible type 'PolarVector *'
非常感谢(我希望这不是重复的——我找不到任何关于 Objective C 的直接信息)
【问题讨论】:
-
你需要
sortVectorByMagnitude:and:的调用者可以看到交换的结果吗? -
这纯粹是浪费运动:` = [[PolarVector alloc] init];`。如果你要在下一行为你的临时指针赋值,那么创建一个新对象是没有意义的,你只会掉在地板上。
-
您缺少的一点是,虽然您的第二个代码段在做正确的事情(除了虚假的 alloc/init),但更新的值永远不会反映给调用者。
-
rmaddy:是的,结果需要对调用者可见。
-
Hot Licks:是的,非常正确。我想我的做法完全错误。
标签: objective-c pointers swap bubble-sort