函数模板及显式具体化
函数模板及显式具体化:
1 #include<iostream> 2 //#include<climits> 3 #include "stdafx.h" 4 //#include<cmath> 5 //#include<fstream> 6 //#include<cstdlib> 7 //#include<string> 8 //#include<new> 9 10 template <typename T> void wap(T &a, T &b); 11 struct job 12 { 13 char name[20]; 14 double salary; 15 int floor; 16 }; 17 18 template<> void wap<job>(job &j1, job &j2); 19 void show(job &j); 20 21 int main() 22 { 23 using namespace std; 24 cout.precision(3); 25 cout.setf(ios::fixed, ios::floatfield); 26 int i = 10, j = 20; 27 cout << "before swap , the values are :" << endl; 28 cout << "i,j:" << i << "," << j << endl; 29 wap(i,j); 30 cout << "after swap , the values are :" << endl; 31 cout << "i,j:" << i << "," << j << endl; 32 33 job first = { "Tom",5000,5 }; 34 job second = { "Jack",6000,6 }; 35 cout << "before swap , the information is :" << endl; 36 show(first); 37 show(second); 38 wap(first, second); 39 cout << "after swap , the information is :" << endl; 40 show(first); 41 show(second); 42 43 system("pause"); 44 return 0; 45 } 46 47 template <typename T> 48 void wap(T &a, T &b) 49 { 50 T temp; 51 temp = a; 52 a = b; 53 b = temp; 54 } 55 56 57 template<> void wap<job>(job &j1, job &j2) 58 { 59 double t1; 60 t1 = j1.salary; 61 j1.salary = j2.salary; 62 j2.salary = t1; 63 64 int t2; 65 t2 = j1.floor; 66 j1.floor = j2.floor; 67 j2.floor = t2; 68 } 69 70 void show(job& j) 71 { 72 using namespace std; 73 cout << j.name << "has salary :" << j.salary << ",and live in :" << j.floor << "floor" << endl; 74 75 }
浙公网安备 33010602011771号