交换数据时有两种方法,一个传递参数时传递的是地址,另一种是该数据的引用,其本质相同
例子:
void swap(int *num1,int *num2){
int temp;
temp=*num1;
*num1=*num2;
*num2=temp;
}
void swapref(int& a,int& b){
int temp=a;
a=b;
b=temp;
}
int main(){
int num1=4;
int num2=6;
swap(&num1,&num2);
cout<<"num1= "<<num1<<"num2= "<<num2<<"\n";
swapref(num1,num2);
cout<<"num1= "<<num1<<"num2= "<<num2<<"\n";
}