模板:把类型当作未知量来处理,可以忽略类的影响
1 函数模板
模板的声明 :template <typename _Ty> //_Ty可以改,类型的代号 typename可以改为class
调用函数模板:隐式: 正常函数传参
显示: 函数名<类型名>(参数)
函数模板的两种形态:普通函数当作函数模板
类的成员函数当作函数模板
函数模板特殊写法:缺省,存在传常量写法
#include <iostream>
using namespace std;
//int Max(int a, int b)
//{
// return a > b ? a : b;
//}
//string Max(string a, string b)
//{
// return a > b ? a : b;
//}
//引入了模板
template <typename _Ty>//告诉编译器下面代码引入一个位置类型叫做_Ty
_Ty Max(_Ty a, _Ty b)
{
return a > b ? a : b;
}
template <typename A1,typename A2>
void print(A1 a, A2 b)//多个位置类型的写法
{
cout << "两个位置类型" << endl;
}
template <class B1, class B2>
void print(B1 a, B2 b)//多个位置类型的写法
{
cout << a << endl << b << endl;
}
class student
{
public:
template <class S>
void print(S a)
{
cout << a << endl;
}
template <class S1>
void print1(S1 a);
protected:
string name;
int age;
};
template <class S1>//在类外实现函数时template不能省略
void student::print1(S1 a)
{
cout << a << endl;
}
template <class T1,class T2>
void printdata(T1 d1, T2 d2)
{
cout << d1 << endl << d2 << endl;
}
void testpd()//缺省的写法
{
//显示调用可以不传类型,但是参数不能少
printdata("string", 4835);
printdata<string>("lkfn", 6151);
}
template <class T3, size_t size>
void printArry(T3 arry)
{
for (int i = 0; i < size; i++)
{
cout << arry[i];
}
cout << endl;
}
int main()
{
//隐式调用
cout << Max(8, 9) << endl;
cout << Max("kjsdb", "snoish") << endl;
//显示调用
cout << Max<string>("ndos", "ofhos") << endl;
print<string, int>("str", 99);
//类中的成员函数是函数模板
student A;
A.print(3);
A.print1<int>(9);
//函数模板只能传入常量
int arry[3] = { 9,8,7 };
printArry<int*, 3>(arry);
return 0;
}
2 类模板
template<class T>
class A
{
public:
protected:
}//只要被template修饰就是类模板
类模板的调用必须采用显示调用
类模板不是一个实际类型,所以所有用到类名的地方都要使用类名<未知类型>方式使用
在多文件中类模板的声明和实现要在一起
template <class T1>
class student
{
public:
student() {}
student(string name) :name(name) {}//构造函数正常写
void print();
protected:
string name;
};
//在类外实现
template <class T1>
void student<T1>::print()
{
cout << "类模板" << endl;
}
template <class T1>//类模板的继承
class book :public student<T1>
{
public:
book(string name) :student<T1>(name) {}
};
template <class T1, class T2>
class Data
{
public:
Data(T1 one, T2 two) :one(one), two(two) {}
void print();
protected:
T1 one;
T2 two;
};
template <class T1, class T2>
void Data<T1, T2>::print()
{
cout << one << endl << two << endl;
}
int main()
{
// student A;//报错
student<int>A;//必须采用显示调用
student<int> book("jdfho");
Data<string, int> A("sfh'", 18);
A.print();
Data<int, int>B(9, 8);
B.print();
return 0;
}
3 模板传入自定义类型
基本自定义类型
自定义类型也是一个模板
模板传入自定义类型,关键在于重载运算符
class student
{
public:
student(string name, int age) :name(name), age(age) {}
friend ostream& operator<<(ostream& out, student& S)
{
out << S.name << endl << S.age << endl;
return out;
}
protected:
string name;
int age;
};
template <class T1>
void print(T1 one)
{
cout << one << endl;
}
int main()
{
print(2);
print<string>("ldjfn");
print(student("hhh", 18));
return 0;
}
4 模板嵌套
template <class s1,class s2>
class student
{
public:
student(s1 one, s2 two) :one(one), two(two) {}
friend ostream& operator<<(ostream& out, student& S)
{
out << S.one << endl << S.two << endl;
return out;
}
protected:
s1 one;
s2 two;
};
template <class s1, class s2>
class Data
{
public:
Data(s1 one, s2 two) :one(one), two(two) {}
protected:
s1 one;
s2 two;
};
void testF()
{
Data<student<string, int>, student<string, int>>
data(student<string, int>("lksd", 18), student<string, int>("klsafhk", 18));
}
template<class a>
void print(a data)
{
cout << data << endl;
}
int main()
{
//隐式调用
print(student<string, int>("jsaofh", 19));
//显示调用
print<student<string, int>>(student<string, int>("ojdsf", 20));
//起别名
using STty = student<string, int>;
print<STty>(STty("jshos", 18));
return 0;
}
5 模板重载
模板和普通函数,调用函数类型一直到情况下,优先调用普通函数
两个模板同时成立时
6 模板特化
特殊化处理
局部特化
完全特化
template <class s1, class s2>
class student
{
public:
student(s1 one, s2 two) :one(one), two(two) {}
void print()
{
cout << one << endl << two << endl;
}
protected:
s1 one;
s2 two;
};
//局部特化,特殊化
//即将两个类型变为一个类型
template <class s1>
class student<s1,s1>
{
public:
student(s1 one, s1 two) :one(one), two(two) {}
void print()
{
cout << one << endl << two << endl;
}
protected:
s1 one;
s1 two;
};
template<>
//完全特化
class student<string, string>
{
public:
student(string one, string two) :one(one), two(two);
void print()
{
cout << one << endl << two << endl;
}
protected:
string one;
string two;
};
int main()
{
student<string, int> A("lknf", 18);
student<string, string> B("ljdf", "kdfn");
A.print();
B.print();//完全特化的模板,折叠参数
student<string, string> C("ljdf", "padifh");
C.print();
return 0;
}