C++中的引用

闲话少说,先看一个swap交换数字的代码

叫换a,b的值

代码

#include<iostream>
#include<cstdio>
using namespace std;

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;
}

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

原文地址:https://www.cnblogs.com/liudehao/p/5663120.html