我有以下代碼:這個程序爲什麼交換這些值?
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <conio.h>
#include <cstring>
#include <iomanip>
void swap(long a, long b)
{
long temp;
temp=a;
a=b;
b=temp;
}
int _tmain(int argc, _TCHAR* argv[])
{
int x = 5, y = 3;
cout << x ;
cout << y << endl;
swap(x, y);
cout << x ;
cout << y << endl;
getch();
return 0;
}
該計劃使輸出:
5 3
3 5
該計劃實際上是交換價值!這是爲什麼? swap()
的參數不是指針或引用。
(我使用VS 2005)
基本上有些解決,這是http://stackoverflow.com/questions/2712076/how-to的欺騙-use-iterator-in-c/2712125#2712125,雖然你不知道這個,除非你知道答案。 – sbi 2011-05-08 06:14:30