交换两个数

#include <iostream>
#include <stdlib.h>
#include <assert.h>
using namespace std;



void swap1(int a, int b);
void swap2(int* a, int* b);
void swap3(int &a, int &b);

int main()
{
    int m=1,n=2;
    swap1(m,n);
    cout<<m<<" "<<n<<endl;//交换失败

    int p=1,q=2;
    swap2(&p,&q);
    cout<<p<<" "<<q<<endl;//交换成功

    system("pause");
    return 0;
}


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

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


void swap3(int &a, int &b)
{
    int temp = a;
    a = b;
    b=temp;
}
原文地址:https://www.cnblogs.com/dangeal/p/7839476.html