-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_test.cpp
145 lines (129 loc) · 2.42 KB
/
template_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "template_test.h"
#include <iostream>
#if 0
//函数模板原理
template<typename T>
void sweep(T& pa, T& pb)
{
T temp = pa;
pa = pb;
pb = temp;
}
int main()
{
double a1 = 1.11, a2 = 2.22;
sweep(a1,a2);
std::cout << "a1 = " << a1;
return 0;
}
#endif
#if 0
//函数模板的实例化
//1.隐式实例化:让编译器根据实参(调用的参数)推演模板参数的实际类型
template<typename T>
T ADD(const T& left, const T& right)
{
return left + right;
}
int main()
{
int a1 = 10,a2 = 20;
double b1 = 1.11,b2 = 2.22;
// 隐式实例化
std::cout << ADD(a1,a2) << std::endl;
std::cout <<ADD(b1,b2) << std::endl;
//显示调用
std::cout <<ADD<int>(b1,b2) << std::endl;
return 0;
}
#endif
#if 0
template <typename T>
class Vector
{
public:
Vector(size_t capacity = 10)
: _pData(new T[capacity]), _size(0), _capacity(capacity)
{
}
~Vector();
void PushBack(const T &data)
{
if (_size == _capacity)
{
T *p = new T[_capacity * 2];
for (int i = 0; i < _size; ++i)
{
p[i] = _pData[i];
}
_pData[_size] = data;
++_size;
}
}
private:
T *_pData;
size_t _size;
size_t _capacity;
};
// 类模板成员函数在类外定义,必须加上类模板的参数列表
template <typename T>
Vector<T>::~Vector() // 必须指定类域,~Vector是属于Vector<T>类模板的成员函数
{
if (_pData)
delete[] _pData;
_size = 0;
_capacity = 0;
}
int main()
{
// 模板实例化
Vector<int> v1(1);
v1.PushBack(1);
v1.PushBack(2);
v1.PushBack(3);
v1.PushBack(4);
return 0;
}
#endif
////////////////////////////////////////////
#if 0
#include <string>
template <class T1, typename T2>
class Pair
{
public:
T1 key;
T2 value;
Pair(T1 k, T2 v) : key(k), value(v){};
bool operator<(const Pair<T1, T2> &p) const;
};
template <class T1, typename T2>
bool Pair<T1, T2>::operator<(const Pair<T1, T2> &p) const
{
return key < p.key;
}
int main()
{
Pair<std::string, int> student("Tom", 19);
std::cout << student.key << " " << student.value;
return 0;
}
#endif
#if 1
template <class T>
class A
{
public:
template <class T2>
void Func(T2 t)
{
std::cout << t << std::endl;
}
};
int main()
{
A<int> a;
a.Func("k");
a.Func("hello");
}
#endif