您的位置:首页 > 其它

采用值传递为什么2值不交换呢?

2012-12-22 19:29 239 查看
#include<iostream>

using namespace std;

void swapr(int &a,int &b);

void swapp(int *q,int *p);

void swapv(int a,int b);

int main()

{

system ("color d");

int a,b;

cout<<" 原始的两值为:";

cin>>a>>b;

cout<<"a="<<a<<' '<<"b="<<b<<endl;

cout<<"采用引用传递后:";

swapr(a,b);

cout<<"a="<<a<<' '<<"b="<<b<<endl;

cout<<"采用指针传递后:";

swapp(&a,&b);

cout<<"a="<<a<<' '<<"b="<<b<<endl;

cout<<" 采用值传递后:";

swapv(a,b);

cout<<"a="<<a<<' '<<"b="<<b<<endl;

return 0;

}

void swapr(int &a,int &b)

{

int t;

t=a;

a=b;

b=t;

}

void swapp(int *q,int *p)

{

int t;

t=*q;

*q=*p;

*p=t;

}

void swapv(int a,int b)

{

int t;

t=a;

a=b;

b=t;

}

采用值传递,为什么a和b没有交换值,看了书也没弄明白,知道的大侠详细的说下(我自学C++,所以.....)!

听君一句话,胜读十年书!!

谢谢你们了!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐