C++ 算法一 交换算法


#include "stdafx.h"
#include <iostream>

//C语言
//宏函数 交换
#define SWAP(x,y,t) ((t)=(x),(x)=(y),(y)=(t))

// 引用方式
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;
}
		//typename
template<class  T>
void swap_template(T &x, T &y)
{
	int temp;
	temp = x;
	x = y;
	y = temp;
}

int main()
{
	int a, b;
	a = 1;
	b = 10;
	std::swap(a,b);

	//swap_template(a, b);
	std::cout << "a=" << a << " b= " << b << std::endl;

	system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37981386/article/details/81433362