/*指针版交换*/ #include <iostream> using namespace std; void swap(int *a,int *b) { int temp; temp=*a; *a=*b; *b=temp; } int main() { int i=3,j=5; cout<<"before swap:"<<"i="<<i<<",j="<<j<<endl; swap(&i,&j); cout<<"after swap:"<<"i="<<i<<",j="<<j<<endl; return 0; }
/*引用版交换*/ #include <iostream> using namespace std; void swap(int &a,int &b) { int temp; temp=a; a=b; b=temp; }int main() { int a, b; cin>> a>> b; swap(a, b); cout<< "a="<< a<< " "<< "b="<< b<< endl; system("pause"); return 0; }