#include<iostream>
#include<Windows.h>
#include<string>
using namespace std;
template<typename T>
bool IsEqual(const T& left, const T& right)
{
return left == right;
}
void test1()
{
string s1("s1"), s2("s2");
cout<< IsEqual(s1, s2)<< endl;
cout<< IsEqual(1, 1)<< endl;
}
int main()
{
test1();
system("pause");
return 0;
}
模板参数匹配及显示实例化
<span style="font-family: SimHei; font-size: 14px;">template<typename T>
bool IsEqual(const T& left, const T& right)
{
return left == right;
}
void test1()
{
cout << IsEqual(1, 1) << endl;
cout << IsEqual<int>(1, 1.2) << endl; // 显示实例化
cout << IsEqual<double>(1, 1.2) << endl; // 显示实例化
}
int main()
{
test1();
system("pause");
return 0;
}</span>
模板类
类模板和函数模板都是以template开始后接模板形参列表组成,模板形参不能为空,一但声明了类模板
就可以用类模板的形参名声明类中的成员变量和成员函数,即可以在类中使用内置类型的地方都可以使
用模板形参名来声明。比如
template<class T> class A{public: T a; T b; T hy(T c, T &d);};
在类A中声明了两个类型为T的成员变量a和b,还声明了一个返回类型为T带两个参数类型为T的函数hy。
类模板对象的创建:比如一个模板类A,则使用类模板创建对象的方法为A<int> m;在类A后面跟上一个
<>尖括号并在里面填上相应的类型,这样的话类A中凡是用到模板形参的地方都会被int 所代替。当类
模板有两个模板形参时创建对象的方法为A<int, double> m;类型之间用逗号隔开。
对于类模板,模板形参的类型必须在类名后的尖括号中明确指定。举个例子A<1> m; 这样是绝对不行的
,记住类模板中不存在实参类型的推断。
在类模板外部定义成员函数的方法为:
template<模板形参列表> 函数返回类型 类名<模板形参名>::函数名(参数列表){函数体},
比如有两个模板形参T1,T2的类A中含有一个void h()函数,则定义该函数的语法为:
template<class T1,class T2> void A<T1,T2>::h(){}。
注意:当在类外面定义类的成员时template后面的模板形参应与要定义的类的模板形参一致。
下面是我用模板写的一个简易的链表:
<span style="font-family: SimHei; font-size: 14px;">template<class T>
struct ListNode
{
ListNode<T>* _next;
ListNode<T>* _prev;
T _date;
ListNode(const T& x)
:_data(x)
, _next(NULL)
, _prev(NULL)
{}
};
template<class T>
class List
{
typedef ListNode<T> Node;
public:
List()
:_head(NULL)
, _tail(NULL)
{}
List(const List<T>& l)
:_head(NULL)
, _tail(NULL)
{
Node* cur = l._head;
while (cur)
{
PushBack(cur->data);
cur = cur->_next;
}
}
List<T>& operator=(List<T> l)
{
Swap(l);
return *this;
}
void Swap(List<T>& l)
{
swap(_head, l._head);
swap(_tail, l._tail);
}
~List()
{
Clear();
}
void clear()
{
Node* cur = _head;
while (cur)
{
Node* next = cur->_next;
delete cur;
cur = next;
}
_head = _tail = NULL;
}
void PushBack(cinst T& x)
{
if (_head == NULL)
{
_head = _tail = new Node(x);
}
else{
Node* tmp = new Node(x);
_tail->_next = tmp;
tmp->_prev = _tail;
_tail = tmp;
}
}
void PopBack()
{
if (_head == NULL)
{
return;
}
else if (_head == _tail)
{
delete _tail;
_head = _tail = NULL;
}
else
{
Node* prev = _tail->_prev;
delete _tail;
_tail = prev;
_tail->_next = NULL;
}
}
void PushFront(const T& x)
{
assert(_head != NULL);
if (_head == NULL)
{
PushBack(x);
}
else
{
Node* cur1 = new Node(x);
cur1->next = _head;
_head->_prev = cur1;
_head = cur1;
}
}
PopFront()
{
if (_head == NULL)
{
return;
}
else if (_head == _tail)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node* next = _head->_next;
delete _head;
_head = next;
}
}
T& Front()
{
assert(_head);
return _head->_date;
}
T& Back()
{
assert(_tail);
return _tail->_date;
}
size_t Size()
{
size_t count = 0;
Node* cur = _head;
while (cur)
{
++count;
cur = cur->_next;
}
return count;
}
bool Empty()
{
return NULL == _head;
}
void Insert(Node* pos, const T& x)
{
asert(pos != NULL);
if (NULL == _head)
{
PushBack(x);
}
else if (_head == _tail || pos == _head)
{
PopBack(x);
}
else
{
Node* cur = new Node(x);
Node* cur2 = pos->_next;
cur2->_prev = pos;
cur->_next = cur2;
pos->_next = cur;
cur->_prev = pos;
}
}
void Erase(Node* pos)
{
assert(_head != NULL);
if (_head == _tail)
{
PopFront();
}
else
{
Node* cur = _head;
while (cur->_next != pos)
{
cur = cur->_next;
}
Node* del = pos;
cur->_next = pos->_next;
pos->_next->_prev = cur;
delete del;
}
}
Node* Find(const T& x)
{
Node* cur = _head;
while (cur)
{
if (cur->_date == x)
{
return cur;
}
cur = cur->_next;
}
return 0;
}
void Print()
{
Node* cur = _head;
while (cur)
{
cout << cur->_date << " ";
cur = cur->_next;
}
cout << endl;
}
protected:
Node* _head;
Node* _tail;
};
</span>