基本类型
#include<iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
void swap(int x,int y);
swap(a,b);
cout<<a<<b<<endl;
return 0;
}
void swap(int x,int y)
{
int t;
t=x,x=y,y=t;
cout<<x<<y<<endl;
}
//输入1,2
//输出21
//输出12
指针类型
#include<iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
void swap2(int *x,int *y);
swap2(&a,&b);
cout<<a<<b<<endl;
return 0;
}
void swap2(int *x,int *y)
{
int t;
t=*x,*x=*y,*y=t;
cout<<*x<<*y<<endl;
}
//输入1,2
//输出21
//输出21
本文深入探讨了在C++中使用基本类型和指针类型进行变量交换的两种方法。通过具体的代码示例,展示了如何利用临时变量实现数值的互换,并对比了直接交换和通过指针交换的不同之处。
8712

被折叠的 条评论
为什么被折叠?



