【发布时间】:2011-05-16 16:38:45
【问题描述】:
在一些 comp-sci 论文和测试中,我看到 swap() 是这样实现的:
void swap(int x, int y, int *a)
{
int t = a[x];
a[x] = a[y];
a[y] = t;
}
为什么不简单地实现它:
void swap(int& x, int& y)
{
int t = x;
x = y;
y = t;
}
前者的想法是通过不必为前两个参数索引到数组中来使调用代码更简洁吗?我意识到这不是一个非常重要的问题,因为我们应该使用 std::swap(),但我仍然很好奇。
【问题讨论】:
标签: c++ c algorithm computer-science