printf("ho_tari\n");

Swap2 (template 사용) 본문

C++

Swap2 (template 사용)

호타리 2023. 9. 8. 15:11

<main.cpp>

#include <iostream>

#include "swap.h"



int main()

{

	int i = 100, j = 200;

	swap(i, j);

	

	std::cout << "i: " << i << ", j: " << j << std::endl;

	

	double d = 1.1, f = 2.2;

	swap(d, f);

	

	std::cout << "d: " << d << ", f: " << f << std::endl;



	//swap(i, d);



	return 0;

}

<swap.h>

#ifndef SWAP_H

#define SWAP_H



template <typename T>

void swap(T& ra, T& rb)

{

	T tmp = ra;

	ra = rb;

	rb = tmp;

}



#endif

 

<compile 결과>

'C++' 카테고리의 다른 글

BoundArray2 (template 사용)  (0) 2023.09.08
SafeArray3 (template 사용)  (0) 2023.09.08
Queue4 (template 지정)  (0) 2023.09.08
Stack4 (template 지정)  (0) 2023.09.08
Array2 (template 지정)  (0) 2023.09.08