C++中用引用的方法交换两个数??

来源:百度知道 编辑:UC知道 时间:2024/06/07 15:07:34
设计函数swap,交换main函数中的a,b值。要求运用引用的方法!!#include<iostream.h>
void swap( )
{

}
void main()
{
int a,b;
cin>>a>>b;
swap(a,b);
cout<<a<<" "<<b <<endl;
}

强人们帮忙把swap函数填写完整~~~ 谢了!!!
#include<iostream.h>
void swap(int *p1,int *p2)
{
int p;
p=*p1;
*p1=*p2;
*p2=p;
}
void main()
{
int a,b;
cin>>a>>b;
swap(&a,&b);
cout<<a<<" "<<b <<endl;
}

我是这样写的,但是我的这个写法没有运用到引用的方法……
另外问一下, *和&的区别是什么? 谢了

函数传递有传值,传址,你那个不是引用阿,是传地址,stomachcao是传值,呵呵,具体找本书把,都有讲,去吃饭了
=========================
void swap(int &a,int &b )
{
int temp;
temp=a;
a=b;
b=temp;
}

void swap(int a,int b )
{
int temp;
temp=a;
a=b;
b=temp;
}

引用一个中间变量