|
#include <iostream>
using namespace std;
void swap_1 (int a, int b){
int t;
t=a;
a=b;
b=t;
}
void swap_2 (int &a, int &b){
int t;
t=a;
a=b;
b=t;
}
void swap_3 (int *a, int *b){
int t;
t=*a;
*a=*b;
*b=t;
}
int main()
{
int x=10,y=20;
cout << "Before x="<< x << " y=" << y << endl;
swap_1(x,y);
cout << "After x="<< x << " y=" << y << endl;
int w=30,v=40;
cout << "Before w="<< w << " v=" << v << endl;
swap_2(w,v);
cout << "After w="<< w << " v=" << v << endl;
int m=70,n=80;
cout << "Before m="<< m << " n=" << n << endl;
swap_3(&m,&n);
cout << "After m="<< m << " n=" << n << endl;
return 0;
}
普通swap via pointer大概是这样,但我只能做到20 30变成30 20, 做不到who变ohw这种。在那里能够找到这一类型的例子呢?我想做一些参考。 |
|