Creative Code

swap2(template 함수) 본문

C++ Programming

swap2(template 함수)

빛하루 2023. 9. 8. 15:12

※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; 

	return 0;

}

※swap.h파일

#ifndef SWAP_H

#define SWAP_H



template <typename T>  	

void swap(T &ra, T &rb) {   // template함수 

	T tmp = ra;

	ra = rb;

	rb = tmp;

}



#endif

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

genderRatio.cpp(static_cast)  (0) 2023.09.08
safeArray4(try-throw-catch)  (0) 2023.09.08
boundArray2(template사용)  (0) 2023.09.08
safeArray3(template 사용)  (0) 2023.09.08
queue4(template사용)  (0) 2023.09.08