模版就是建议通用的磨具,大大提高复用性
函数模版作用:
建立一个通用幻术,其函数返回值类型和形参类型可以1不具体规定,用一个虚拟的类型来代表。
语法:
template
函数声明或定义
解释:
template --- 声明创建模版
typename ---表面其后面的符号是一种数据类型,可以用class代替
T ---通用的数据类型,名称可以替换,通常为大写字母
#include using namespace std; //两个整型交换函数 void swapInt(int& a, int& b) { int temp = a; a = b; b = temp; } //交换两个浮点型函数 void swapDouble(double& a, double& b) { double temp = a; a = b; b = temp; } //函数模版 template //声明一个模版,告诉编译器后面代码中紧跟着的T不要报错,T是一个通用数据类型 void mySwap(T& a, T& b) { T temp = a; a = b; b = temp; } void test01() { int a = 10; int b = 20; //swapInt(a, b); //利用函数模版实现交换 //有两种方式使用函数模版 //1.自动类型推导 mySwap(a, b); cout << "a=" << a << " b=" << b << endl; double c = 1.1; double d = 2.2; //swapDouble(c, d); //2.显示指定类型 mySwap(c, d); cout << "c=" << c << " d=" << d << endl; } int main() { test01(); system("pause"); return 0; }
注意事项:
#include using namespace std; //函数模版注意事项 template void mySwap(T& a, T& b) { T temp = a; a = b; b = temp; } //1.自动类型推导,必须推导出一致的数据类型T才可以使用 void test01() { int a = 10; int b = 20; char c = 'c'; mySwap(a, b);//正确 //mySwap(a, c);//错误,不能自动推导出一致的数据类型 cout << "a=" << a << endl; cout << "b=" << b << endl; } //2.模版必须确定出T的数据类型,才可以使用 template void func() { cout << "func()调用" << endl; } void test02() { //func();无法确定T的数据类型,错误 func(); } int main() { test01(); system("pause"); return 0; }
总结
案例描述:
#include using namespace std; //实现一个通用的对数组进行排序 //规则 从大到小 //算法 选择排序 //测试 char数组、 int数组 //交换函数模版 template void mySwap(T& a, T& b) { T temp = a; a = b; b = temp; } //排序算法 template void mySort(T arr[],int len) { for (int i = 0; i < len; i++) { int max = i;//认定最大值的下标 for (int j = i + 1; j < len; j++) { //认定的最大值比遍历出的数据要小,说明j下标的元素才是真正的最大值 if (arr[max] < arr[j]) { max = j;//更新最大值 } } if (max != i) { //交换max和i元素 mySwap(arr[max], arr[i]); } } } //提供打印数组的模版 template void printArray(T arr[], int len) { for (int i = 0; i < len; i++) { cout << arr[i] << " "; } cout << endl; } void test01() { //测试char数组 char charArr[] = "badcfe"; int num = sizeof(charArr) / sizeof(char); mySort(charArr, num); printArray(charArr, num); } void test02() { //测试int数组 int intArr[] = { 5, 3, 8, 6, 2, 7, 1, 4 }; int num = sizeof(intArr) / sizeof(int); mySort(intArr, num); printArray(intArr, num); } int main() { test01(); test02(); system("pause"); return 0; }
#include using namespace std; //普通函数和函数模版的区别 //1.普通函数调用时可以发生自动类型转换(隐式类型转换) //2.函数模版调用时,如果利用自动类型推导,不会发生隐式类型转换 //3.如果利用显示指定类型的方式,可以发生隐式类型转换 //普通函数 int myAdd01(int a,int b) { return a + b; } //函数模版 template T myAdd02(T a, T b) { return a + b; } void test01() { int a = 10; int b = 20; char c = 'c'; cout << myAdd01(a, c) << endl; //自动类型转换,c自动转换为int类型 a 97 c 99 //自动类型推导,不会发生隐式类型转换 //cout << myAdd02(a, c) << endl; //a 10 c 99 错误 //显示指定类型,可以发生隐式类型转换 cout << myAdd02(a, c) << endl; //a 10 c 99 正确 } int main() { test01(); system("pause"); return 0; }
调用规则如下:
#include using namespace std; //1.如果函数模版和普通函数都可以实现,优先调用普通函数 //2.可以通过空模版参数列表来强制调用函数模版 //3.函数模版也可以发生重载 //4.如果函数模版可以产生更好的匹配,优先调用函数模版 void myPrint(int a, int b) { cout << "普通函数调用" << endl; } template void myPrint(T a, T b) { cout << "调用的模版" << endl; } template void myPrint(T a, T b, T c) { cout << "调用重载的模版" << endl; } void test01() { int a = 10; int b = 20; myPrint(a, b); //优先调用普通函数 //通过空模版的参数列表,强制调用函数模版 myPrint<>(a, b); //调用的模版 myPrint(a, b, 100);//调用重载的模版 //4.如果函数模版可以产生更好的匹配,优先调用函数模版 char c1 = 'a'; char c2 = 'b'; myPrint(c1, c2); //调用的模版 } int main() { test01(); system("pause"); return 0; }
总结:既然提供了函数模版,最好就不要提供普通函数,否则容易出现二义性
局限性:模版的通用性并不是万能的
#include using namespace std; //模版局限性 //模版并不是万能的,有些特定的数据类型,需要用具体化方式做特殊实现 class Person { public: //构造函数 Person(string name, int age) { m_name = name; m_age = age; } //姓名 string m_name; //年龄 int m_age; }; //对比两个数据是否相等 template bool myCompare(T& a, T& b) { if (a == b) { return true; } else { return false; } } //利用具体化的Person的版本,具体化优先调用 template<> bool myCompare(Person& a, Person& b) { if (a.m_name == b.m_name && a.m_age == b.m_age) { return true; } else { return false; } } void test01() { int a = 10; int b = 20; bool result = myCompare(a, b); if (result) { cout << "a==b" << endl; } else { cout << "a!=b" << endl; } } void test02() { Person p1("Tom", 10); Person p2("Tom", 10); bool ret = myCompare(p1, p2); if (ret) { cout << "p1==p2" << endl; } else { cout << "p1!=p2" << endl; } } int main() { test01(); test02(); system("pause"); return 0; }
类模版作用:
语法:template
类
#include using namespace std; //类模版 template class Person { public: Person(NameType name, AgeType age) { this->m_Name = name; this->m_Age = age; } NameType m_Name; AgeType m_Age; void showPerson() { cout << "name:" << this->m_Name << "age:" << this->m_Age << endl; } }; void test01() { Person p1("Tom", 20); p1.showPerson(); } int main() { test01(); system("pause"); return 0; }
类模版和与函数模版的区别主要有两点:
1.类模版没用自动类型推导的使用方式
2.类模版在模版参数列表中可以有默认参数
#include using namespace std; //类模版和函数模版的区别 template class Person { public: Person(NameType name, AgeType age) { this->m_name = name; this->m_age = age; } void showPerson() { cout << "name:" << this->m_name << "age:" << this->m_age << endl; } NameType m_name; AgeType m_age; }; //1.类模版没有自动类型推导的方式 void test01() { //Person p("Tom", 20); 编译器无法自行推导,没有自动类型转换 //必须显示声明 Person p("Tom", 20);//正确 只能显示指定 p.showPerson(); } //2.类模版在模版的参数列表中可以有默认参数 //template void test02() { //Person p1("Tom",999); 正确 } int main() { test01(); system("pause"); return 0; }
类模版中成员函数和普通类中成员函数创建时机是有区别的:
#include using namespace std; //类模版中成员函数创建时机 //类模板中的成员函数在调用时才创建 class Person1 { public: void showPerson1() { cout << "Person1 show" << endl; } }; class Person2 { public: void showPerson2() { cout << "Person2 show" << endl; } }; template class MyClass { public: T obj; //类模版中的成员函数 void func1() { obj.showPerson1(); } void func2() { obj.showPerson2(); } }; //编译通过,因为没有调用 void test01() { MyClass m; m.func1(); //m.func2(); } int main() { test01(); system("pause"); return 0; }
学习目标:
一共有三种传入方式:
#include using namespace std; //类模版的对象做函数参数 template class Person { public: Person(T1 name, T2 age) { this->m_name = name; this->m_age = age; } T1 m_name; T2 m_age; void showPerson() { cout<<"姓名:"<m_name<<" 年龄:"<m_age<& p) { p.showPerson(); } void test01() { Personp("孙悟空", 100); printPerson(p); } //2.参数模板化 template void printPerson2(Person& p) { p.showPerson(); cout << "T1的类型为:" << typeid(T1).name() << endl; cout << "T2的类型为:" << typeid(T2).name() << endl; } void test02() { Personp("猪八戒", 90); printPerson2(p); } //3.整个类模板化 template void printPerson3(T& P) { P.showPerson(); cout<<"T的数据类型为:"<p("唐僧", 30); printPerson3(p); } int main() { test01(); test02(); test03(); system("pause"); return 0; }
当类模版碰到继承时,需要注意以下几点
#include using namespace std; //类模版与继承 template class Base { T m; }; //class Son :public Base 错误,必须要知道父类中的T类型,才能继承给子类 class Son :public Base { }; void test() { Son s1; } //2.如果想要灵活的指定父类中的T类型,子类也需要变类模版 template class Son2:public Base{ T1 obj; }; void test2() { Son2 s2; } int main(){ system("pause"); return 0; }
总结:类模版中成员函数类外实现是,需要加上模版参数列表
#include using namespace std; //类模版中的成员函数的类外实现 template class Person { public: Person(T1 name, T2 age);// //this->m_Name = name; //this->m_Age = age; // void showPerson();// //cout<<"姓名:"<m_Name<<"年龄"<m_Age // T1 m_Name; T2 m_Age; }; //构造函数的类外实现(类模版) template Person::Person(T1 name, T2 age) { this->m_Name = name; this->m_Age = age; } template void Person::showPerson() { //cout << "姓名:" << this->m_Name << "年龄" << this->m_Age < endl; cout << "姓名:" << this->m_Name << "年龄:" << this->m_Age << endl; } void test01() { Person p1("张三", 99); p1.showPerson(); } int main() { test01(); system("pause"); return 0; }
问题:
解决:
person.hpp的代码
#pragma once #include using namespace std; template class Person { public: Person(T1 name, T2 age); void showPerson(); T1 m_name; T2 m_age; }; template Person::Person(T1 name, T2 age) { this->m_name = name; this->m_age = age; } template void Person::showPerson() { cout << "姓名:" << this->m_name << endl; cout << "年龄:" << this->m_age << endl; }
person.cpp代码
#pragma once #include using namespace std; template class Person { public: Person(T1 name, T2 age); void showPerson(); T1 m_name; T2 m_age; }; template Person::Person(T1 name, T2 age) { this->m_name = name; this->m_age = age; } template void Person::showPerson() { cout << "姓名:" << this->m_name << endl; cout << "年龄:" << this->m_age << endl; }
#include //#include "person.cpp" //第一种解决方式直接包含源文件 //第二种解决方式,将.h和.cpp中的内容写到一起,将后缀改为.hpp文件 #include "person.hpp" using namespace std; //类模版的分文件编写问题及解决 //template //class Person { //public: // Person(T1 name, T2 age); // void showPerson(); // // T1 m_name; // T2 m_age; //}; //template //Person::Person(T1 name, T2 age) { // this->m_name = name; // this->m_age = age; //} // //template //void Person::showPerson() { // // cout<<"姓名:"<m_name<m_age< p("Tom", 66); p.showPerson(); } int main() { test(); system("pause"); return 0; }
全局函数类内实现-直接在类内声明友元即可
全局函数类外实现-需要提前让编译器知道全局函数的存在
#include using namespace std; // 通过全局函数打印Person的信息 template class Person; //类外实现 template void printPerson2(Person p) { cout << "类外-Name: " << p.m_Name << endl; cout << "类外-Age: " << p.m_Age << endl; } template class Person { //全局函数类内实现 friend void printPerson(Person p) { cout << "Name: " << p.m_Name << endl; cout << "Age: " << p.m_Age << endl; } //全局函数 类外实现 //加空模版的参数列表 //如果全局函数是类外实现的话,需要让编译器提前知道这个函数的存在 friend void printPerson2<>(Person p); public: Person(T1 name, T2 age) { this->m_Name = name; this->m_Age = age; } private: T1 m_Name; T2 m_Age; }; //1.全局函数在类内实现 void test01() { Person p("Tom", 20); printPerson(p); } 类外实现 //template //void printPerson2(Person p) { // cout << "类外-Name: " << p.m_Name << endl; // cout << "类外-Age: " << p.m_Age << endl; // // // //} //2.全局函数类外实现测试 void test02() { Person p("jack", 40); printPerson2(p); } int main() { test01(); test02(); system("pause"); return 0; }
案例描述: