记录学习过程
#include<iostream>
using namespace std;
//要实现两个数进行交换,共有三个函数
//函数1 两个形参的值进行交换
void swap1(int i, int j) {
int temp;
temp = i;
i = j;
j = temp;
}
//函数2 两个指针变量的值进行交换
void swap2(int* i, int* j) {
int* temp;
temp = i;
i = j;
j = temp;
}
//函数3 两个指针内的值进行交换
void swap3(int* i, int* j) {
int temp;
temp = *i;
*i = *j;
*j = temp;
}
int main() {
int a = 10;
int b = 20;
cout << "交换前:" << endl;
cout << "a = " << a << " b = " << b << endl;
//结果为 a = 10 b = 20
swap1(a, b);
cout << "交换后1:" << endl;
cout << "a = " << a << " b = " << b << endl;
//结果为 a = 10 b = 20 未实现交换
swap2(&a, &b);
cout << "交换后2:" << endl;
cout << "a = " << a << " b = " << b << endl;
//结果为 a = 10 b = 20 未实现交换
swap3(&a, &b);
cout << "交换后3:" << endl;
cout << "a = " << a << " b = " << b << endl;
//结果为 a = 20 b = 10 实现交换
return 0;
}