目录
一、模板定义
C++另一种编程思想称为泛型编程,主要利用的技术是模板
C++提供两种模板机制:函数模板和类模板
二、函数模板
2.1 使用
1.建立一个通用函数,其函数返回值和形参类型可以不具体制定,用一个虚拟的类型代表(类型参数化)
2.两种方式:自动类型推导、显示指定类型
#include <iostream>
using namespace std;
template <class T>
void my_swap(T& a, T& b)
{
T temp;
temp = a;
a = b;
b = temp;
}
int main()
{
int a = 1;
int b = 2;
//自动类型推导
my_swap(a,b);
cout << "a=" << a << " " << "b=" << b << endl;
double c = 0.5;
double d = 1.2;
//显示指定类型
my_swap<double>(c,d);
cout << "c=" << c << " " << "d=" << d << endl;
return 0;
}
2.2 注意事项
1.自动类型推导,必须推导出一致的数据类型T,才可以使用(不会发生隐式类型转换)
template <class T>
void my_swap(T& a, T& b)
{
T temp;
temp = a;
a = b;
b = temp;
}
int main()
{
int a = 1;
int b = 2;
char f = 'w';
my_swap(a,f); //T推断不出是int or char
cout << "a=" << a << " " << "b=" << b << endl;
return 0;
}
2.模板必须要确定出T的数据类型,才可以使用 (会发生隐式类型转换)
template <class T>
void func()
{
cout << "func()函数调用" << endl;
}
int main()
{
//func(); //err,T的数据类型没有确定
func<int>(); //使用指定类型
return 0;
}
2.3 普通函数与函数模板的调用规则
1.如果函数模板和普通函数都可以实现,优先调用普通函数;
2.可以通过空模板参数列表来强制调用函数模板;
3. 函数模板也可以发生重载;
4.如果函数模板可以产生更好的匹配,优先调用函数模板;
#include <iostream>
using namespace std;
void myPrint(int a, int b)
{
cout << "调用的函数" << endl;
}
template<class T>
void myPrint(T a, T b)
{
cout << "调用的模板" << endl;
}
template<class T>
void myPrint(T a, T b, T c)
{
cout << "调用的重载模板" << endl;
}
int main()
{
int a = 10;
int b = 20;
//1.如果函数模板和普通函数都可以实现,优先调用普通函数;
myPrint(a, b); //输出:调用的函数
//2.可以通过空模板参数列表来强制调用函数模板;
myPrint<>(a, b); //输出:调用的模板
//3. 函数模板也可以发生重载;
myPrint(a, b,100); //输出:调用的重载模板
//4.如果函数模板可以产生更好的匹配,优先调用函数模板;
char c = 'w';
char d = 'y';
myPrint(c,d); //输出:调用的模板
return 0;
}
2.4 模板局限性
模板并不是万能的,有些特定数据类型,需要用具体化方式做特殊实现
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(string name,int age)
{
_name = name;
_age = age;
}
string _name;
int _age;
};
template<class T>
bool Compare(T& a, T&b)
{
if (a == b)
return 1;
else
return 0;
}
int main()
{
int a = 10;
int b = 10;
cout << Compare(a, b) << endl; //可以判断成功
Person p1("Tom",10);
Person p2("Jerry", 10);
cout << Compare(p1, p2) << endl; //err,Person为自定义类型,不可判断
return 0;
}
解决方法:
//利用具体化Person的版本实现代码,具体化优先调用
template<> bool Compare(Person& a, Person& b)
{
if (a._name == b._name && a._age == b._age)
return 1;
else
return 0;
}
三、类模板
3.1 类模板和函数模板的区别
1.类模板使用只能用显示指定类型方式;
2.类模板中的模板参数列表可以有默认参数;
#include <iostream>
#include <string>
using namespace std;
//1.
template<class NameType, class AgeType>
//2.模板参数列表有默认参数
template<class NameType, class AgeType = int>
class Person
{
public:
Person(NameType name, AgeType age)
{
_name = name;
_age = age;
}
NameType _name;
AgeType _age;
};
//1.类模板使用只能用显示指定类型方式;
void test01()
{
//Person p1("小明",12); //err
Person<string, int> p1("小明", 12);
}
//2.类模板中的模板参数列表可以有默认参数;
void test02()
{
Person<string> p2("小王", 25);
}
int main()
{
test01();
test02();
return 0;
}
3.2 类模板中成员函数的创建时机
类模板中成员函数和普通类中成员函数创建时机是有区别的:
1.普通类中的成员函数一开始就可以创建
2.类模板中的成员函数在调用时才创建
#include <iostream>
#include <string>
using namespace std;
class Person1
{
public:
void showPerson1()
{
cout << "Person1 show" << endl;
}
};
class Person2
{
public:
void showPerson2()
{
cout << "Person2 show" << endl;
}
};
template<class T>
class MyPerson
{
public:
T obj;
void func1()
{
obj.showPerson1();
}
void func2()
{
obj.showPerson2();
}
};
int main()
{
MyPerson<Person1> mp;
mp.func1();
//mp.func2(); //err
return 0;
}
3.3 类模板对象做函数参数
#include <iostream>
#include <string>
using namespace std;
template<class T1,class T2>
class Person
{
public:
Person(T1 name,T2 age)
{
_name = name;
_age = age;
}
void showPerson()
{
cout << "姓名:" << _name << " "<<"年龄:" << _age << endl;
}
T1 _name;
T2 _age;
};
//1.传入指定类型
void PrintPerson1(Person<string, int>& p)
{
p.showPerson();
}
void test01()
{
Person<string, int>p1("孙悟空",100);
PrintPerson1(p1);
}
//2.参数模板化
template<class T1,class T2>
void PrintPerson2(Person<T1, T2>& p)
{
p.showPerson();
}
void test02()
{
Person<string, int>p2("猪八戒", 90);
PrintPerson2(p2);
}
//3.整个类模板化
template<class T>
void PrintPerson3(T& p)
{
p.showPerson();
}
void test03()
{
Person<string, int>p3("唐僧", 80);
PrintPerson3(p3);
}
int main()
{
test01();
test02();
test03();
return 0;
}
3.4 类模板与继承
当子类继承的父类是一个类模板时,
1.子类在声明的时候,要指定出父类中T的类型如果不指定,编译器无法给子类分配内存;
2.如果想灵活指定出父类中T的类型,子类也需变为类模板;
#include <iostream>
#include <string>
using namespace std;
template<class T>
class Base
{
public:
T m;
};
//1.Son1继承父类需要指定父类T为int或其他类型
class Son1: public Base<int>
{
public:
int a;
};
//2.Son2继承需要子类也变为模板(在template中加上父类的T)
//Son2实例化对象时也需为父类的T指明类型
template<class T1, class T2>
class Son2 : public Base<T2>
{
T1 b;
};
int main()
{
Son2<int, char> s2;
return 0;
}
3.5 类模板成员函数类外实现
#include <iostream>
#include <string>
using namespace std;
template<class T1,class T2>
class Person
{
public:
Person(T1 name, T2 age);
void PrintPerson();
T1 _name;
T2 _age;
};
//构造函数类外实现
template<class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
_name = name;
_age = age;
}
//成员函数类外实现
template<class T1, class T2>
void Person<T1, T2>::PrintPerson()
{
cout << "姓名:" << _name << " " << "年龄:" << _age << endl;
}
int main()
{
Person<string,int> p1("小米",12);
p1.PrintPerson();
return 0;
}