1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| #include<iostream> using namespace std;
void Swap01(int a, int b) { int temp = a; a = b; b = temp; }
void Swap02(int* a, int* b) { int temp = *a; * a = *b; * b = temp; }
void Swap03(int &a, int &b) { int temp = a; a = b; b = temp; }
int main() { int a = 10; int b = 20; Swap01(a, b); cout << "值传递交换" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "\n地址传递交换" << endl; Swap02(&a, &b); cout << "a = " << a << endl; cout << "b = " << b << endl;
Swap03(a, b); cout << "\n引用传递交换" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl;
return 0; }
|