void swap(int *a,int *b) 没看懂 :(
#include <iostream>
using namespace std;
void swap(int *a,int *b)
{
int *t;
t = a;
a = b;
b = t;
}
int main()
{
int x=3,y=5;
int *p=&x,*q=&y;
swap(x,y); // 5 3 5 3
//swap(p,q);// 3 5 3 5
//swap(&x,&y);//3 5 3 5
//swap(*p,*q);// 5 3 5 3
cout<<x<<" "<<y<<" "<<*p<<" "<<*q<<endl;
return 0;
}
2、void swap(int &a,int &b)
#include <iostream>
using namespace std;
void swap(int &a,int &b)
{
int t;
t = a;
a = b;
b = t;
}
int main()
{
int x=3,y=5;
int *p=&x,*q=&y;
// swap(x,y); // 5 3 5 3
swap(p,q);// 3 5 5 3
// swap(*p,*q);// 5 3 5 3
cout<<x<<" "<<y<<" "<<*p<<" "<<*q<<endl;
return 0;
}