指针形参,引用变量,普通变量比较

 

#include<iostream>
using namespace std;
void swapOne(int *p1,int *p2)
{
cout<<"swapOne old p1 = "<<*p1<<"   "<<"swapOne old p2 = "<<*p2<<endl;
int t;
t = *p1;
*p1 = *p2;
*p2 = t;
cout<<"swapOne new p1 = "<<*p1<<"   "<<"swapOne new p2 = "<<*p2<<endl;
}
void swapTow(int &p1,int &p2)
{
cout<<"swapTow old p1 = "<<p1<<"    "<<"swapTow new p2 = "<<p2<<endl;
int t;
t = p1;
p1 = p2;
p2 = t;
cout<<"swapTow new p1 = "<<p1<<"   "<<"swapTow new p2 = "<<p2<<endl;
}
void swapThree(int x,int y)
{
cout<<"swapThree old x: "<<x<<"   "<<"swapThree old y: "<<y<<endl;
int t;
t = x;
x = y;
y = t;
cout<<"swapThree new x: "<<x<<"   "<<"swapThree new y: "<<y<<endl;
}
int main()
{
int a = 5;
int b = 26;
cout<<"old a = "<<a<<"   "<<"old b = "<<b<<endl;
//swapOne(&a,&b);
// swapTow(a,b);
swapThree(a,b);
cout<<"new a = "<<a<<"   "<<"new b = "<<b<<endl;
return 0;
}
阅读更多
换一批

没有更多推荐了,返回首页