본문 바로가기
컴퓨터/C++

[정렬][C++] Template를 이용한 Swap 함수

by Luyin 2013. 1. 30.

[정렬][C++]

Swap Function(스왑 함수)

 template<typename T>
void swap(T& _input1, T& _input2)
{
	T Temp = _input1;
	_input1 = _input2;
	_input2 = Temp;
}


예제)

 int main(void)
{

int a=5, b=7; char c = 'c', d = 'd'; swap(a, b); swap(c, d); cout << "a = "<< a << ", b = "<< b << endl; cout << "c = "<< c << ", d = "<< d << endl; return 0; }