会飞的蝌蚪君

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

函数模板及显式具体化

 

函数模板及显式具体化:

 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 }

 

posted on 2018-03-25 17:16  会飞的蝌蚪  阅读(125)  评论(0)    收藏  举报