(C/C++学習ノート)汎用プログラミング基礎
2287 ワード
#include "iostream"
using namespace std;
void swap(int &a, int &b)
{
int c ;
c = a;
a = b;
b = c;
}
void swap(float &a, float &b)
{
float c ;
c = a;
a = b;
b = c;
}
void main11()
{
int a = 1, b = 2;
swap(a, b);
float a1 = 1, b1 = 2;
swap(a1, b1);
system("pause");
}
//template c++ ,
//typename c++ ,T (T , ,int float),
//
template<typename T>
void swap2(T &a, T &b)
{
T c;
c = a;
a = b;
b = c;
}
void main()
{
//
//
int x =1, y =2;
swap2(x, y);
printf("x:%d y:%d
", x, y);
float x1= 1.0, y1 = 2.0;
//
swap2<float>(x1, y1);
printf("x1:%f y1:%f
", x1, y1);
system("pause");
}