#include <iostream>
using namespace std;
//值交换
void swap1(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
//地址交换
void swap2(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
//引用交换
void swap3(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int a = 1;
int b = 2;
swap1(a, b);
cout << a << "," << b << endl;
swap2(&a, &b);
cout << a << "," << b << endl;
swap3(a, b);
cout << a << "," << b << endl;
return 0;
}
C++的三种交换数值的方式(值传递、地址传递、引用传递)
最新推荐文章于 2023-02-23 01:26:25 发布