作用:建立一个通用的函数,其函数返回值类型和形式参数可以不具体制定,用一个虚拟的类型去表示
语法:template
解释:
template ---- 声明创建模板
typename ----表明后面的符号是一种类型
T ----通用的数据类型
示例:
#include2、函数模板的注意事项using namespace std; template void mySwap(T &a,T &b) { T temp=a; a = b; b = temp; } int main() { int a = 10; int b = 20; //mySwap(a, b); mySwap (a, b); cout << a << endl; cout << b << endl; system("pause"); return 0; }
注意事项:
-
自动类型推导,必须推导出一致的数据类型
-
模板必须要确定T的数据类型才可以使用
#include3、函数模板的案例-实现排序using namespace std; template void mySwap(T &a,T &b) { T temp=a; a = b; b = temp; } template void fun() { cout << "void fun()的调用" << endl; } void test() { fun (); } int main() { int a = 10; int b = 20; mySwap(a, b); char c = 'c'; mySwap (a, b); //mySwap (a, c); cout << a << endl; cout << b << endl; system("pause"); return 0; }
#include4、普通函数和函数模板的区别using namespace std; template void mySwap(T &a,T &b) { T temp=a; a = b; b = temp; } template void mySort_Max_To_Min(T arr[], int len) { for (int i = 0; i < len; i++) { int max = i; for (int j = i + 1; j < len; j++) { if (arr[max] < arr[j]) { max = j; } } if (max != i) { mySwap(arr[max], arr[i]); } } } template void mySort_Min_To_Max(T arr[], int len) { for (int i = 0; i < len; i++) { int max = i; for (int j = i + 1; j < len; j++) { if (arr[max] > arr[j]) { max = j; } } if (max != i) { mySwap(arr[max], arr[i]); } } } int main() { int a = 10; int b = 20; //mySwap(a, b); //char c = 'c'; //mySwap (a, b); mySwap (a, c); //cout << a << endl; //cout << b << endl; char charArray[] = "badcfe"; int arraySize = sizeof(charArray) / sizeof(charArray[0]); mySort_Max_To_Min(charArray, arraySize); mySort_Min_To_Max(charArray, arraySize); for (int i = 0; i < arraySize; i++) { cout << charArray[i]; } cout << endl; system("pause"); return 0; }
- 普通函数调用时可以发生自动类型转换(隐式类型转换)
- 函数模板调用时,自动类型推导,不会发生隐式类型转换
- 函数模板调用时,显示指定类型,可以发生隐式类型转换
1、如果普通函数和函数模板都可以实现,优先调用普通函数
2、可以通过空模板参数列表来强制调用函数模板
3、模板函数也可以发生重载
4、如果函数模板可以产生更好的匹配效果(不要进行隐式类型转换),优先调用函数模板
#include6、函数模板的局限性using namespace std; void fun(int a, int b) { cout << "普通函数调用:" << endl; } template void fun(T a, T b) { cout << "模板函数调用:" << endl; } template void fun(T a, T b, T c) { cout << "重载模板调用" << endl; } int main() { //fun(a, b); //fun<>(a, b); //fun(a, b, 100); char a = 'a'; char c = 'c'; fun(a, c); system("pause"); return 0; }
对于自定义的数据类型,模板无法正常使用,必须需要重载
#include2、类模板 1、类模板语法using namespace std; class Person { public: Person(string name, int age) { this->name = name; this->age = age; } string name; int age; }; template bool myCompare(T& a, T& b) { if (a == b) { return true; } else { return false; } } template<>bool myCompare(Person& p1, Person& p2) { if (p1.name == p2.name&&p1.age==p2.age) { return true; } else { return false; } } void test01() { int a = 20; int b = 20; bool ret= myCompare(a, b); if (ret) { cout << "相等" << endl; } else { cout << "不相等" << endl; } } void test02() { Person p1("Tom", 20); Person p2("Tom", 20); bool ret = myCompare(p1,p2); if (ret) { cout << "相等" << endl; } else { cout << "不相等" << endl; } } int main() { //test01(); test02(); system("pause"); return 0; }
类模板作用:建立一个通用的类,类中的成员数据类型不具体制定,用一个虚拟的类型来代表
语法:template 类
#include2、类模板与函数模板区别using namespace std; template class Person { public: Person(nameType name, ageType age) { this->name = name; this->age = age; } nameType name; ageType age; }; void test() { Person p1("孙悟空", 9999); cout << "姓名:" << p1.name << " 年龄:" << p1.age << endl; } int main() { test(); system("pause"); return 0; }
1、类模板必须显示指定类型
2、类模板可以有默认参数
#include3、类模板中成员函数的创建时机using namespace std; template class Person { public: Person(nameType name, ageType age) { this->name = name; this->age = age; } nameType name; ageType age; }; void test() { Person p1("孙悟空", 9999);//只能指定数据类型 cout << "姓名:" << p1.name << " 年龄:" << p1.age << endl; } void test2() { Person p1("孙悟空", 9999);//有默认数据类型int cout << "姓名:" << p1.name << " 年龄:" << p1.age << endl; } int main() { test2(); system("pause"); return 0; }
- 普通函数的成员函数在一开始就可以创建
- 类模板的成员函数需要调用时才创建
#include4、类模板对象做函数参数using namespace std; class Person1 { public: void fun1() { cout << "void fun1()调用:" << endl; } }; class Person2 { public: void fun2() { cout << "void fun2()调用:" << endl; } }; template class myClass { public: T obj; void f1() { obj.fun1(); } void f2() { obj.fun2(); } }; int main() { myClass p; //p.f1(); p.f2(); system("pause"); return 0; }
1、指定参数类型
2、参数模板化
3、整个类模板化
#includeusing namespace std; #include template class Person { public: Person(NameType name,AgeType age) { this->name = name; this->age = age; } void showPerson() { cout << "姓名:" << this->name << " 年龄:" << this->age << endl; } NameType name; AgeType age; }; //1、指定传入类型 void printPerson01(Person &p) { p.showPerson(); } void test01() { Person p("孙悟空", 999); printPerson01(p); } //2、参数模板化 template void printPerson02(Person &p) { p.showPerson(); cout << "T1的类型:" << typeid(nameType).name() << endl; cout << "T2的类型:" << typeid(ageType).name() << endl;//这是一个函数 } void test02() { Person p("猪八戒", 998); printPerson02(p); } //3、整个类都模板化 template void printPerson03(T& p) { p.showPerson(); cout << "T的类型:" << typeid(T).name() << endl; } void test03() { Person p("唐僧", 30); printPerson03(p); } int main() { //test01(); //test02(); test03(); system("pause"); return 0; }



