我有以下代码:这个程序为什么交换这些值?
#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