#include <iostream>
#include <string>
using namespace std;
void swap1(int *, int *);
void swap2(int &,int &);
int main(){
int x = 1, y = 2;
int *p = &x;
int *q = &y;
swap1(p,q); //若原型是*p,*q則寫p,q即可。
cout<<" x is "<<x<<" and y is "<<y<<endl;
swap2(x,y); //若原型是&x,&y則寫x,y即可。
cout<<" x is "<<x<<" and y is "<<y<<endl;
swap1(&x,q);//若原型是*p,*q也可以寫&x,&y。
cout<<" x is "<<x<<" and y is "<<y<<endl;
swap2(*p,y);//若原型是&x,&y則也可以寫*p,*q。
cout<<" x is "<<x<<" and y is "<<y<<endl;
return 0;
}
void swap1(int *p, int *q){
int temp = 0;
temp = *p;
*p = *q;
*q = temp;
}
void swap2(int &i, int&j){
int temp = 0;
temp = i;
i = j;
j = temp;
} |