#include <QCoreApplication>
#include <iostream>
/*
@brief:C++数据结构--实现交换算法
*/
using namespace std;
void swap(int x,int y)
{//交换失败,原因是值传递--就是把参数进行拷贝,把x 拷贝,y 拷贝,交换的是拷贝,原来的没有影响
int temp;
temp=x;
x=y;
y=temp;
}
//C 语言实现
void swap1(int *x,int *y)
{ //利用指针实现地址传递
int temp;
temp=*x;
*x=*y;
*y=temp;
}
//C++ 实现
void swap2(int &x,int &y)
{
//使用引用传递,就是给变量起了一个别名
int temp;
temp=x;
x=y;
y=temp;
}
//C++ 实现利用模板函数实现
//template<class T> void swap(T &x,T &y);
int main(int argc, char *argv[])
{
// QCoreApplication a(argc, argv);
cout<<"hello"<<endl;
int a=1,b=2;
cout<<"change before:"<<a<<b<<endl;
//swap(a,b);//交换之后
//swap1(&a,&b)
// swap2(a,b);
std::swap(a,b);//使用的是C++指针函数
cout<<a<<b<<"after change"<<endl;
}