本阶段主要针对C++泛型编程和ST技术做详细讲解,探讨C++更深层的使用
模板就是建立通用的模具,大大提高复用性
模板的特点:
模板不可以直接使用,它只是一个框架
模板的通用并不是万能的
C++另一种编程思想称为 泛型编程,主要利用的技术就是模板
C++提供两种模板机制:函数模板和类模板
函数模板作用:
建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个虚拟的类型来代表,
语法:
- template<typename T>
-
- 函数声明或定义
解释:
template--- 声明创建模板
typename---表面其后面的符号是一种数据类型,可以用class代替
T ---通用的数据类型,名称可以替换,通常为大写字母
示例:
- #include<iostream>
- using namespace std;
-
- template<typename T>>//声明一个模板,告诉编译器后面代码中紧跟着的T不要报错,T是一个通用的数据类型
- void myswap(T &a,T &b)
- {
- T temp = a;
- a = b;
- b = temp;
- }
-
-
- void test01()
- {
- int a = 10;
- int b = 20;
-
- //1,自动类型推导
- cout << "第一次交换:自动类型推导" << endl;
- myswap(a, b);
- cout << "a=" << a << endl;
- cout << "b=" << b << endl;
-
- //2,显示指定类型
- cout << "第二次交换:显示指定类型" << endl;
- myswap<int>(a, b);
- cout << "a=" << a << endl;
- cout << "b=" << b << endl;
-
- }
-
- int main()
- {
- test01();
-
- system("pause");
- return 0;
- }

总结:
函数模板利用关键字 template
使用函数模板有两种方式:自动类型推导、显示指定类型
模板的目的是为了提高复用性,将类型参数化
注意事项:
自动类型推导,必须推导出一致的数据类型T,才可以使用
模板必须要确定出T的数据类型,才可以使用
示例:
- #include<iostream>
- using namespace std;
-
- template<typename T>//typename可以替换成class
- void myswap(T& a, T& b)
- {
- T temp = a;
- a = b;
- b = temp;
- }
-
- void test01()
- {
- int a = 10;
- int b = 20;
-
- //1,自动类型推导,必须推导出一致的数据类型T, 才可以使用
- cout << "第一次交换:自动类型推导" << endl;
- myswap(a, b);//正确
- cout << "a=" << a << endl;
- cout << "b=" << b << endl;
-
- cout << "第二次交换:显示指定类型" << endl;
- myswap<int>(a, b);
- cout << "a=" << a << endl;
- cout << "b=" << b << endl;
-
- }
- //2,模板必须要确定出T的数据类型,才可以使用
- template<class T>
- void func()
- {
- cout << "func调用" << endl;
- }
-
- void test02()
- {
- //func();错误,模板不能独立使用,必须确定出T的模型才可以
- func<int>();//利用显示指定类型的方式,给T一个类型,才可以使用该模板
- }
-
- int main()
- {
- test01();
-
- test02();
- system("pause");
- return 0;
- }

案例描述:
利用函数模板封装一个排序的函数,可以对不同数据类型数组进行排序
排序规则从大到小,排序算法为选择排序
分别利用char数组和int数组进行测试
- #include<iostream>
- using namespace std;
-
- //利用函数模板封装一个排序的函数,可以对不同数据类型数组进行排序
- //规则 从小到大
- //算法 选择
- //测试 char,数组,int 数组
-
- //打印函数模板
- template<typename T>
- void printArray(T arr[], int len)
- {
- for (int i = 0; i < len; i++)
- {
- cout << arr[i] << " ";
- }
- cout << endl;
- }
-
- //交换函数模板
- template<class T>
- void mySwap(T &a, T &b)
- {
- T temp = a;
- a = b;
- b = temp;
- }
-
- //排序算法
- template<class T>
- 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]);
- }
- }
- }
-
- void test02()
- {
- int intArr[] = { 7,5,1,3,9,2,4,6,8 };
- int num = sizeof(intArr) / sizeof(intArr[0]);
- mySort(intArr, num);
- printArray(intArr, num);
- }
-
- void test01()
- {
- //测试char数组
- char charArr[7] = "badcef";
- int num = sizeof(charArr) / sizeof(char);
- mySort(charArr, num);
- printArray(charArr, num);
- }
-
- int main()
- {
- test01();
-
- test02();
-
- system("pause");
- return 0;
- }
普通函数与函数模板区别
普通函数调用时可以发生自动类型转换(隐式类型转换)
函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换
如果利用显示指定类型的方式,可以发生隐式类型转换
示例:
- #include<iostream>
- using namespace std;
-
- //普通函数与函数模板的区别
-
- //普通函数调用时可以发生自动类型转换(隐式类型转换)
- //函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换
- //如果利用显示指定类型的方式,可以发生隐式类型转换
-
- //普通数组
- int myAdd01(int a,int b)
- {
- return a + b;
- }
-
- //函数模板
- template<class T>
- T myAdd02(T a, T b)
- {
- return a + b;
- }
-
- void test01()
- {
- int a = 10;
- int b = 20;
- char c = 'c';
- cout << "a + b =" << myAdd01(a, b) << endl;
- cout <<"a + c =" << myAdd01(a, c) << endl;//a - 97 c - 99
-
- //自动类型推导
- cout << "自动类型推导" << endl;
- cout << myAdd02(a, b) << endl;
- //cout << myAdd02(a, c) << endl;//报错,不会发生隐式类型转换
-
- //显示指定类型
- cout << "显示指定类型" << endl;
- cout << myAdd02<int>(a, c) << endl;
- //会发生隐式类型转换
- }
-
- int main()
- {
-
- test01();
- system("pause");
-
- return 0;
- }

总结:建议使用显示指定类型的方式,调用函数模板,因为可以自己确定通用类型
调用规则如下:
1.如果函数模板和普通函数都可以实现,优先调用普通函数
2.可以通过空模板参数列表来强制调用函数模板
3.函数模板也可以发生重载
4.如果函数模板可以产生更好的匹配,优先调用函数模板
示例1:
- #include<iostream>
- using namespace std;
-
- //1.如果函数模板和普通函数都可以实现,优先调用普通函数
- void myprint(int a, int b)
- {
- cout << "调用的普通函数" << endl;
- }
-
- template<class T>
- void myprint(T a, T b)
- {
- cout << "调用的模板函数" << endl;
- }
-
- void test01()
- {
- int a = 10;
- int b = 10;
- myprint(a, b);
- }
-
- int main()
- {
-
- test01();
- system("pause");
-
- return 0;
- }

示例2:
- #include<iostream>
- using namespace std;
- //2.可以通过空模板参数列表来强制调用函数模板
-
- void myprint(int a, int b)
- {
- cout << "调用的普通函数" << endl;
- }
-
- template<class T>
- void myprint(T a, T b)
- {
- cout << "调用的模板函数" << endl;
- }
-
- void test01()
- {
- int a = 10;
- int b = 10;
- myprint<>(a, b);
- }
-
- int main()
- {
-
- test01();
- system("pause");
-
- return 0;
- }

示例3:
- #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;
- }
-
- void test01()
- {
- int a = 10;
- int b = 10;
-
- //3,函数也可以发生重载
- int c = 10;
- myprint(a, b, c);
-
- //4,如果函数模板可以产生更好的匹配,优先调用函数模板
- char c1 = 'a';
- char c2 = 'c';
- myprint(c1,c2);
-
- }
-
- int main()
- {
-
- test01();
- system("pause");
-
- return 0;
- }
总结:既然提供了函数模板,最好就不要提供普通函数,否则容易出现二义性
局限性:
模板的通用性并不是万能的
例如:
- template<class T>
- void f(T a, T b)
- {
- a = b;
- }
在上述代码中提供的赋值操作,如果传入的a和b是一个数组,就无法实现了
再例如:
- template<class T>
- void f(T a, T b)
- {
- if(a>b)
- {
- ...
- }
- }
在上述代码中,如果T的数据类型传入的是像Person这样的自定义数据类型,也无法正常运行
因此C++为了解决这种问题,提供模板的重载,可以为这些特定的类型提供具体化的模板
- #include<iostream>
- using namespace std;
- #include<string>
-
- //模板的局限性
- //模板并不是万能的,有些特定数据类型,需要用具体化方式做特殊实现
-
- class Person
- {
- public:
- Person(string name, int age)
- {
- this->m_Name = name;
- this->m_age = age;
- }
- string m_Name;
- int m_age;
- };
-
-
- template<class T>
- bool myCompare(T &a,T &b)
- {
- if (a == b)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
- //利用具体化Person版本实现代码,具体化优先调用
- template<>bool myCompare(Person& p1, Person& p2)
- {
- if (p1.m_Name == p2.m_Name && p1.m_age == p2.m_age)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
- void test01()
- {
- int a = 10;
- int b = 20;
- bool ret = myCompare(a, b);
- if (ret)
- {
- 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;
- }

总结:
利用具体化的模板,可以解决自定义类型的通用化
学习模板不是为了写模板,而是在STL能够运用系统提供的模板
类模板作用:
建立一个通用类,类中的成员 数据类型可以不具体制定,用一个虚拟的类型来代表。
- //语法:
-
- template<typename T>
- 类
解释:
template…声明创建模板
typename -表面其后面的符号是一种数据类型,可以用class代替
T… 通用的数据类型,名称可以替换,通常为大写字母
- #include<iostream>
- using namespace std;
- #include<string>
-
- //类模板
- template<class NameYtpe,class AgeType>
- class Person
- {
- public:
- Person(NameYtpe name, AgeType age)
- {
- this->m_Name = name;
- this->m_Age = age;
- }
-
- void ShowPerson()
- {
- cout << "name: " << this->m_Name <<"\t" << "age: " << this->m_Age << endl;
- }
-
- NameYtpe m_Name;
- AgeType m_Age;
- };
-
-
-
- void test01()
- {
- Person<string, int>p1("孙悟空", 99);
- p1.ShowPerson();
- }
-
- int main()
- {
- test01();
- system("pause");
- return 0;
- }
总结:类模板和函数模板语法相似,在声明模板template后面加类,此类称为类模板
类模板与函数模板区别主要有两点:
1.类模板没有自动类型推导的使用方式
2.类模板在模板参数列表中可以有默认参数
示例:
- #include<iostream>
- using namespace std;
- #include<string>
-
- //类模板与函数模板的区别
- template<class NameYtpe, class AgeType = int>
- class Person
- {
- public:
- Person(NameYtpe name, AgeType age )
- {
- this->m_Name = name;
- this->m_Age = age;
- }
-
- void ShowPerson()
- {
- cout << "name: " << this->m_Name << "\t" << "age: " << this->m_Age << endl;
- }
-
- NameYtpe m_Name;
- AgeType m_Age;
- };
-
- //1.类模板没有自动类型推导的使用方式
- void test01()
- {
- //Person p("孙悟空",1000);
- Person<string, int>p1("孙悟空", 99);//只能用显示指定类型
- p1.ShowPerson();
- }
- //2.类模板在模板参数列表中可以有默认参数
- void test02()
- {
- Person<string>p2("猪八戒", 999);
- p2.ShowPerson();
- }
-
-
- int main()
- {
- test01();
- test02();
- system("pause");
- return 0;
- }
类模板中成员函数和普通类中成员函数创建时机是有区别的:
普通类中的成员函数一开始就可以创建
类模板中的成员函数在调用时才创建
- // 类模板中成员函数创建时机
- //普通类中的成员函数一开始就可以创建
-
- //类模板中的成员函数在调用时才创建
-
- #include<iostream>
- 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 Myclass
- {
- public:
-
- T obj;
-
- //类模板中的成员函数,并不是一开始就创建的,而是在模板调用时再生成
- //类模板中的成员函数
- void func1()
- {
- obj.showPerson1();
- }
- void func2()
- {
- obj.showPerson2();
- }
- };
-
- void test01()
- {
- Myclass<Person1>m;
- m.func1();
- //m.func2();//编译会出错,说明函数调用时才会去创建成员函数
- }
-
- int main()
- {
- test01();
- system("pause");
- return 0;
- }

学习目标:
类模板实例化出的对象,向函数传参的方式
共有三种传入方式:
1.指定传入的类型 直接显示对象的数据类型
2.参数模板化 将对象中的参数变为模板进行传递
3. 将这个对象类型 模板化进行传递3.整个类模板化
示例:
- #include<iostream>
- using namespace std;
- #include<string>
- //类模板对象做函数参数
-
- template<class T1, class T2>
- class Person
- {
- public:
- Person(T1 name, T2 age)
- {
- this->m_Name = name;
- this->m_Age = age;
- }
-
- void showPerson()
- {
- cout << "name: " << this->m_Name << "\t" << "age: " << this->m_Age << endl;
- }
-
- T1 m_Name;
- T2 m_Age;
- };
-
- void printPerson1(Person<string,int>&p)
- {
- p.showPerson();
- }
-
- //1,指定传入类型
- void test01()
- {
- Person<string ,int>p("孙悟空", 100);
- printPerson1(p);
- }
-
- //2,参数模板化
- template<class T1, class T2>
- void printPerson2(Person<T1, T2>& p)
- {
- p.showPerson();
- cout << "T1的类型为:" << typeid(T1).name() << endl;
- cout << "T1的类型为:" << typeid(T2).name() << endl;
- }
-
-
- void test02()
- {
- Person<string, int>p("猪八戒", 90);
- printPerson2(p);
- }
-
- //3,整个类模板化
- template<class T>
- void printPerson3(T &p)
- {
- p.showPerson();
- cout << "T1的类型为:" << typeid(T).name() << endl;
- }
-
- void test03()
- {
- Person<string, int>p("唐增", 90);
- printPerson3(p);
- }
-
- int main()
- {
-
- test01();
-
- test02();
-
- test03();
-
- system("pause");
- return 0;
- }

总结:
通过类模板创建的对象,可以有三种方式想函数中进行传参
使用比较广泛的是第一种:指定传入的类型
当类模板碰到继承时,需要注意一下几点:
当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T类型
如果不指定,编译器无法给子类分配内存
如果想灵活指定出父类中T的类型,子类也需变为类模板
- #include<iostream>
- using namespace std;
- #include<string>
-
- //类模板与继承
- template<class T>
- class Base
- {
- T m;
- };
-
- //class Son:public Base//错误,必须要指定父类中的T类型,才能继承给子类
- class Son1 :public Base<int>
- {
-
- };
-
- void test01()
- {
- Son1 s1;
- }
-
- //如果想灵活指定出父类中T的类型,子类也需变为类模板
- template<class T1,class T2>
- class Son2 :public Base<T2>
- {
- public:
- Son2()
- {
- cout << "T1的类型为:" << typeid(T1).name() << endl;
- cout << "T2的类型为:" << typeid(T2).name() << endl;
- }
- T1 obj;
- };
-
- void test02()
- {
- Son2<int, char>S2;
- }
-
-
- int main()
- {
-
- //test01();
-
- test02();
-
- //test03();
-
- system("pause");
- return 0;
- }

总结:如果父类是类模板,子类需要指定出父类中T的数据类型
示例:
- #include<iostream>
- using namespace std;
- #include<string>
-
- template<class T1, class T2>
- class Person
- {
- public:
- Person(T1 name, T2 age);
-
- void ShowPerson();
-
- T1 m_Name;
- T2 m_Age;
- };
-
- //构造函数的类外实现
- template<class T1, class T2>
- Person<T1,T2>::Person(T1 name, T2 age)
- {
- this->m_Name = name;
- this->m_Age = age;
- }
-
- //成员函数类外实现
- template<class T1, class T2>
- void Person<T1, T2>::ShowPerson()
- {
- cout << "name: " << this->m_Name << "\t" << "age: " << this->m_Age << endl;
- }
-
- void test01()
- {
- Person<string, int>P("Tom", 20);
- P.ShowPerson();
- }
-
- int main()
- {
-
- test01();
-
- system("pause");
- return 0;
- }

总结:类模板中成员函数类外实现时,需要加上模板参数列表
学习目标:
掌握类模板成员函数分文件编写产生的问题以及解决方式
问题:
类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到
解决:
解决方式1:直接包含.cpp源文件
解决方式2:将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制
我们首先将类模板成员函数类外实现的文件分为以下三个文件:
Person.h
- #include"Person.h"
- //构造函数的类外实现
- template<class T1, class T2>
- Person<T1, T2>::Person(T1 name, T2 age)
- {
- this->m_Name = name;
- this->m_Age = age;
- }
-
- //成员函数类外实现
- template<class T1, class T2>
- void Person<T1, T2>::ShowPerson()
- {
- cout << "name: " << this->m_Name << "\t" << "age: " << this->m_Age << endl;
- }
Person.cpp
- #pragma once
- #include<iostream>
- using namespace std;
- #include<string>
-
- //类模板分文件编写问题以及解决
- template<class T1, class T2>
- class Person
- {
- public:
- Person(T1 name, T2 age);
-
- void ShowPerson();
-
- T1 m_Name;
- T2 m_Age;
- };
-
类模板分文件编写.cpp
- #include"person.h"
- void test01()
- {
- Person<string, int>P("Tom", 20);
- P.ShowPerson();
- }
-
- int main()
- {
- test01();
- system("pause");
- return 0;
- }
解决方法1:将类模板的头文件改为:#include"person.cpp"即可运行
解决方法2:将.h和.cpp中的内容写到一起,将后缀名改为.hpp文件

总结:主流的解决方式是第二种,将类模板成员函数写到一起,并将后缀名改为.hpp
学习目标:
掌握类模板配合友元函数的类内和类外实现
全局函数类内实现-直接在类内声明友元即可
全局函数类外实现-需要提前让编译器知道全局函数的存在
示例:
- #include<iostream>
- using namespace std;
- #include<string>
-
- template<class T1, class T2>
- class Person;
-
- //类外实现
- template<class T1, class T2>
- void PrintPerson2(Person<T1, T2>p)
- {
- cout << "类外实现---姓名:" << p.m_Name << "年龄:" << p.m_Age << endl;
- }
-
- //通过全局函数,打印Perosn信息
- template<class T1,class T2>
- class Person
- {
- //全局函数类外实现
- //加空模板参数列表
- //如果全局函数 是类外实现,需要让编译器提前指定这个函数的存在
- friend void PrintPerson2<>(Person<T1, T2>p);
- public:
- //全局函数 类内实现
- friend void PrintPerson(Person<T1, T2>&p)
- {
- cout << "name: " << p.m_Name << "\t" << "age: " << p.m_Age << endl;
- }
-
- Person(T1 name, T2 age)
- {
- this->m_Name = name;
- this->m_Age = age;
- }
- private:
- T1 m_Name;
- T2 m_Age;
- };
-
- void test01()
- {
- Person<string, int>p("Tom", 100);
- PrintPerson(p);
- }
- //全局函数类外实现
- void test02()
- {
- Person<string, int>p("Jerry", 20);
- PrintPerson2(p);
- }
-
- int main()
- {
- test01();
- test02();
- system("pause");
- return 0;
- }

总结:建议全局函数做类内实现,用法简单,而且编译器可以直接识别
1.3.9 类模板案例
案例描述:实现一个通用的数组类,要求如下:
可以对内置数据类型以及自定义数据类型的数据进行存储
将数组中的数据存储到堆区
构造函数中可以传入数组的容量
提供对应的拷贝构造函数以及operator=防止浅拷贝问题
提供尾插法和尾刚法对数组中的教据进行增加和刷除
可以通过下标的方式访问数组中的元素
可以获取数组中当前元素个数和数组的容量
思路:
