template <typename T> 是C++中用于定义模板的固定格式。模板是实现代码重用机制的一种工具,它可以实现类型参数化,即把类型定义为参数, 从而实现了真正的代码可重用性。模版可以分为两类,一个是函数模版,另外一个是类模版。
第一,函数模板。
功能要求:我们需要对int、char、string、double等类型的数据做交换操作,假如没有模板这种重用代码的机制,则我们需要根据不同的参数类型编写多个语句基本相同的函数,有了模板功能,则只需要编写一个函数即可,编译器可以通过输入参数的类型,推断出形参的类型。范例代码如下:
#include<iostream>
#include<string>
using namespace std;
//template 关键字告诉C++编译器 下面是个泛型模板
//数据类型T 参数化数据类型
template <typename T>
void generic_swap(T& a, T& b)
{
cout << "Initial value: " << a << " : " << b << endl;
T tmp;
tmp = b;
b = a;
a = tmp;
}
int main()
{
int a = 100, b = 50;
generic_swap(a, b);
cout << "excute the swap():" << a << " : " << b << endl;
char c = 'A', d = 'B';
generic_swap(c, d);
cout << "excute the swap():" << c << " : " << d << endl;
string e = "Jacky", f = "Lucy";
generic_swap(e, f);
cout << "excute the swap():" << e << " : " << f << endl;
double j = 1.314, k = 5.12;
generic_swap(j, k);
cout << "excute the swap():" << j << " : " << k << endl;
return 0;
}
函数模板范例运行结果
第二,类模板
功能要求:定义一个类来表示坐标,要求坐标的数据类型可以是整数、小数或字符串,例如:
- x = 10、y = 10
- x = 12.88、y = 129.65
- x = "E180"、y = "N210"
这个时候就可以使用类模板如下所示:
#include<iostream>
#include<string>
using namespace std;
//注意:模板头和类头是一个整体,可以换行,但是中间不能有分号
template<typename T1, typename T2> //这里不能有分号
class Point {
public:
Point(T1 x, T2 y) : m_x(x), m_y(y) { }
public:
T1 getX() const; //获取x坐标
void setX(T1 x); //设置x坐标
T2 getY() const; //获取y坐标
void setY(T2 y); //设置y坐标
private:
T1 m_x; //x坐标
T2 m_y; //y坐标
};
//下面就对 Point 类的成员函数进行定义
template<typename T1, typename T2> T1 Point<T1, T2>::getX() const {
return m_x;
}
template<typename T1, typename T2> void Point<T1, T2>::setX(T1 x) {
m_x = x;
}
template<typename T1, typename T2> T2 Point<T1, T2>::getY() const {
return m_y;
}
template<typename T1, typename T2> void Point<T1, T2>::setY(T2 y) {
m_y = y;
}
int main()
{
// 与函数模板不同的是,类模板在实例化时必须显式地指明数据类型
// 编译器不能根据给定的数据推演出数据类型
Point<int, int> p1(10, 10);
cout << "x=" << p1.getX() << ", y=" << p1.getY() << endl;
Point<float, float> p2(12.88, 129.65);
cout << "x=" << p2.getX() << ", y=" << p2.getY() << endl;
Point<string, string> p3("E180","N210");
cout << "x=" << p3.getX() << ", y=" << p3.getY() << endl;
Point<int, float> p4(4, 129.65);
cout << "x=" << p4.getX() << ", y=" << p4.getY() << endl;
Point<string, int> p5("hello,world!", 5);
cout << "x=" << p5.getX() << ", y=" << p5.getY() << endl;
//除了对象变量,我们也可以使用对象指针的方式来实例化
Point<string, int>* p7 = new Point<string, int>("hello,world!", 7);
// (pointer_name)->(variable_name)
// The Dot(.) operator is used to normally access members of a structure or union.
// The Arrow(->) operator exists to access the members of the structure or the unions using pointers
cout << "x=" << p7->getX() << ", y=" << p7->getY() << endl;
delete p7;
return 0;
}
类模板范例运行结果