一、函数模板
1、定义
函数模板作用:建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个虚拟的类型来代表。
#include <iostream> using namespace std; //两个整形交换函数 void swapInt(int &a, int &b) { int temp = a; a = b; b = temp; } //交换两个浮点型函数 void swapFloat(float &a, float &b) { float temp = a; a = b; b = temp; } //函数模板 template<typename T> void mySwap(T &a, T &b) { T temp = a; a = b; b = temp; } void test01() { int a = 10; int b = 20; //1、自动类型推导 mySwap(a, b); //2、显式指定类型 mySwap<int>(a, b); cout << "a=" << a << endl; cout << "b=" << b << endl; } int main() { test01(); system("pause"); return 0; }
a=10 b=20
函数模板使用有两种方式:自动类型推导、显式指定类型。
模板提高了复用性,将类型参数化。
使用模板,数组排序
#include <iostream> using namespace std; //实现通用 对数组进行排序的函数 规则从大到小 算法 选择 测试char数组 int数组 template<class T> void sort(T arr[],int length) { for (int i = 0; i < length; ++i) { T max = arr[i]; for (int j = i + 1; j < length; ++j) { if (arr[j] > arr[i]) { T tem = arr[i]; arr[i] = arr[j]; arr[j] = tem; } } } } void test01(){ int arr1[]={9,4,7,8,99}; char arr2[]={'z','o','y','x','a'}; sort(arr1,sizeof(arr1)/sizeof(arr1[0])); sort(arr2,sizeof(arr2)/sizeof(arr2[0])); for (const auto &item: arr1){ cout<<item<<" "; } cout<<endl; for (const auto &item: arr2){ cout<<item<<" "; } cout<<endl; } int main() { test01(); system("pause"); return 0; }
99 9 8 7 4 z y x o a
2、普通函数与函数模板的调用规则
2.1.如果函数模板和普通函数都可以实现,优先调用普通函数;
2.2.可以通过空模板参数列表来强制调用函数模板;
2.3.函数模板也可以发生重载;
2.4如果函数模板可以产生更好的匹配,优先调用函数模板;
#include <iostream> using namespace std; //普通函数和函数模板调用规则 //1.如果函数模板和普通函数都可以实现,优先调用普通函数; // 2.可以通过空模板参数列表来强制调用函数模板; //3.函数模板也可以发生重载; //4.如果函数模板可以产生更好的匹配,优先调用函数模板; 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 = 20; // 如果函数模板和普通函数都可以实现,优先调用普通函数; myPrint(a, b); // 可以通过空模板参数列表来强制调用函数模板 myPrint<>(a, b); //3.函数模板也可以发生重载; myPrint(1, 2, 3); char c1 = 'a'; char c2 = 'a'; //4.如果函数模板可以产生更好的匹配,优先调用函数模板; myPrint(c1, c2); } int main() { test01(); system("pause"); return 0; }
调用的普通函数 调用模板 调用重载模板 调用模板
二、类模板
1、类模板语法
#include <iostream> #include <string> using namespace std; //类模板 template<class NameType, class AgeType> class Person { public: Person(NameType name, AgeType age) { m_Name = name; m_Age = age; } NameType m_Name; AgeType m_Age; }; void test01() { Person<string,int> p1("zhansan", 15); cout <<p1.m_Name<<" "<< p1.m_Age <<endl; } int main() { test01(); system("pause"); return 0; }
zhansan 15 请按任意键继续. . .
2、类模板与函数模板区别
a.类模板没有自动类型推导的使用方式;
b..类模板在模板参数列表中可以有默认参数;
#include <iostream> #include <string> using namespace std; //类模板 template<class NameType, class AgeType=int> class Person { public: Person(NameType name, AgeType age) { m_Name = name; m_Age = age; } NameType m_Name; AgeType m_Age; }; void test01() { Person<string,int> p1("zhansan", 15); cout <<p1.m_Name<<" "<< p1.m_Age <<endl; Person<string> p2("lisi", 15); cout <<p2.m_Name<<" "<< p2.m_Age <<endl; } int main() { test01(); system("pause"); return 0; }
zhansan 15 lisi 15 请按任意键继续. . .
3、类模板中成员函数创建时机
普通类中的成员函数一开始就可以创建;
类模板中的成员函数在调用时才创建;
#include <iostream> using namespace std; //普通类中的成员函数一开始就可以创建; //类模板中的成员函数在调用时才创建; class Person1 { public: void showPerson1() { cout << "Person 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> p1; p1.func1(); MyClass<Person2> p2; p2.func2(); } int main() { test01(); system("pause"); return 0; }
Person show Person2 show 请按任意键继续. . .
4、类模板对象做函数参数
a.指定传入的类型:直接显示对象的数据类型;
b.参数模板化:将对象中的参数变为模板进行传递;
c.整个类模板化:将这个对象类型模板化进行传递。
#include <iostream> #include <string> using namespace std; //类模板对象做函数参数 template<class T1, class T2> class Person { public: Person(T1 name, T2 age) { this->m_Name = name; this->m_Age = age; } void showPerson() { cout << "姓名:" << this->m_Name << "年龄:" << this->m_Age << endl; } T1 m_Name; T2 m_Age; }; //1、指定传入类型 void printPerson1(Person<string, int> &p) { p.showPerson(); } //2、参数模板化 template<class T1, class T2> void printPerson2(Person<T1, T2> &p) { p.showPerson(); // 打印类型 cout << "T1的类型为:" << typeid(T1).name() << endl; cout << "T2的类型为:" << typeid(T2).name() << endl; } //3、整个类模板化 template<class T> void printPerson3(T &p) { p.showPerson(); cout << "T的类型为:" << typeid(T).name() << endl; } void test01() { Person<string, int> p("sunwukong", 1000); printPerson1(p); } void test02() { Person<string, int> p("zhubajie", 900); printPerson2(p); } void test03() { Person<string, int> p("ts", 900); printPerson3(p); } int main() { // test01(); // test02(); test03(); system("pause"); return 0; }
5、类模板继承
当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T的类型;
如果不指定,编译器无法给子类分配内存;
如果想灵活指定出父类中T的类型,子类也需变为类模板;
#include <iostream> using namespace std; //当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T的类型; template<class T> class Base { T m; }; class Son : public Base<int> { }; //如果不指定,编译器无法给子类分配内存; //如果想灵活指定出父类中T的类型,子类也需变为类模板; template<class T1, class T2> class Son2 : public Base<T2> { T1 obj; }; int main() { system("pause"); return 0; }
6、类模板成员函数类外实现
#include <iostream> using namespace std; //类模板成员函数类外实现 template<class T1, class T2> class Person { public: Person(T1 name, T2 age); // { // this->m_Name = name; // this->m_Age = age; // } void showPerson(); // { // cout << "姓名:" << this->m_Name << "年龄:" << this->m_Age << endl; // } 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 << "姓名:" << this->m_Name << "年龄:" << this->m_Age << endl; } int main() { system("pause"); return 0; }
7、类模板分文件编写
问题:类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到;
解决方式1: 直接包含.cpp源文件;
解决方式2: 将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制(常用)。
main.cpp
#include <iostream> //解决方式1: 直接包含.cpp源文件 //#include "person.cpp" using namespace std; //解决方式2: 将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制 #include "person.hpp" void test01() { Person<string, int> p1("zhangsan", 56); p1.showPerson(); } int main() { test01(); system("pause"); return 0; }
person.h
#pragma once #include <iostream> using namespace std; //问题:类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到; //解决方式1: 直接包含.cpp源文件 //解决方式2: 将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制 //类模板的分文件编写 template<class T1, class T2> class Person { public: Person(T1 name, T2 age); void showPerson(); T1 m_Name; T2 m_Age; };
person.cpp
#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 << "姓名:" << this->m_Name << "年龄:" << this->m_Age << endl; }
person.hpp
#pragma once #include <iostream> using namespace std; //问题:类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到; //解决方式1: 直接包含.cpp源文件 //解决方式2: 将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制 //类模板的分文件编写 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 << "姓名:" << this->m_Name << "年龄:" << this->m_Age << endl; }
8、类模板与友元
掌握类模板配合友元函数的类内和类外实现;
全局函数类内实现- 直接在类内声明友元即可;
全局函数类外实现 - 需要提前让编译器知道全局函数的存在;
#include <iostream> using namespace std; 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; } //通过全局函数 打印Person信息 template<class T1, class T2> class Person { public: //全局函数 类内实现 friend void printPerson1(Person<T1, T2> p) { cout << "姓名:" << p.m_Name << "年龄:" << p.m_Age << endl; } //全局函数 类外实现 friend void printPerson2<>(Person<T1, T2> p); 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("zhangsan", 16); printPerson1(p); }; //全局函数内外实现 void test02() { Person<string, int> p("lisi", 16); printPerson2(p); }; int main() { // test01(); test02(); system("pause"); return 0; }
9、类模板案例
可以对内置数据类型以及自定义数据类型的数据进行存储;
将数组中的数据存储到堆区;
构造函数中可以传入数组的容量;
提供对应的拷贝构造函数以及operator=防止浅拷贝问题;
提供尾插法和尾删法对数组中的数据进行增加和删除
可以通过下标的方式访问数组中的元素;
可以获取数组中当前元素个数和数组的容量;
MyArray.hpp
#pragma once #include <iostream> using namespace std; template<class T> class MyArray { public: //构造函数 MyArray(int capacity) { cout << "有参构造" << endl; this->m_Capactity = capacity; this->m_Size = 0; this->pAddress = new T[this->m_Capactity]; } //拷贝构造 MyArray(const MyArray &arr) { cout << "拷贝构造" << endl; this->m_Capactity = arr.m_Capactity; this->m_Size = arr.m_Size; //深拷贝 this->pAddress = new T[this->m_Capactity]; for (int i = 0; i < this->m_Size; ++i) { this->pAddress[i] = arr.pAddress[i]; } } //operator=防止浅拷贝问题 MyArray &operator=(const MyArray &arr) { cout << "operator=" << endl; //先判断原来堆区是否有数据,如果有先释放 if (this->pAddress != NULL) { delete[] this->pAddress; this->pAddress = NULL; this->m_Capactity = 0; this->m_Size = 0; } // 深拷贝 this->m_Capactity = arr.m_Capactity; this->m_Size = arr.m_Size; this->pAddress = new T[this->m_Capactity]; for (int i = 0; i < this->m_Size; ++i) { this->pAddress[i] = arr.pAddress[i]; } return *this; } //尾插法 void Push_Back(const T &val) { //先判断容量 if (this->m_Capactity == this->m_Size) { return; } //插入尾部,更新数组大小 this->pAddress[this->m_Size] = val; this->m_Size++; } //尾删法 void Pop_Back() { //逻辑删除 if (this->m_Size == 0) { return; } this->m_Size--; } //通过下标方式访问数据中的元素 arr T &operator[](int index) { return this->pAddress[index]; } //返回数组的容量 int getCapacity() { return this->m_Capactity; } //返回数组大小 int getSize() { return this->m_Size; } //析构函数 ~MyArray() { cout << "析构函数" << endl; if (this->pAddress != NULL) { delete[] this->pAddress; this->pAddress = NULL; } } private: T *pAddress; //指针指向堆区开辟的真实数组 int m_Capactity;//数组容量 int m_Size;//数组大小 };
main.cpp
#include <iostream> #include "MyArray.hpp" using namespace std; // 自定义数据类型 class Person { public: Person() {}; Person(string name, int age) { this->m_Name = name; this->m_Age = age; }; string m_Name; int m_Age; }; void pringIntArray(MyArray<int> &arr) { for (int i = 0; i < arr.getSize(); i++) { cout << arr[i] << endl; } } void pringIntPerson(MyArray<Person> &arr) { for (int i = 0; i < arr.getSize(); i++) { cout << arr[i].m_Name<<" " << arr[i].m_Age << endl; } } void test01() { MyArray<int> arr(5); for (int i = 0; i < 10; ++i) { arr.Push_Back(i); } cout << "arr1的打印输出为:" << endl; pringIntArray(arr); cout << "容量:" << arr.getCapacity() << endl; cout << "大小:" << arr.getSize() << endl; arr.Pop_Back(); cout << "容量:" << arr.getCapacity() << endl; cout << "大小:" << arr.getSize() << endl; } void test02() { MyArray<Person> arr(5); for (int i = 0; i < 10; ++i) { Person p("abc", i); arr.Push_Back(p); } cout << "arr1的打印输出为:" << endl; pringIntPerson(arr); cout << "容量:" << arr.getCapacity() << endl; cout << "大小:" << arr.getSize() << endl; arr.Pop_Back(); cout << "容量:" << arr.getCapacity() << endl; cout << "大小:" << arr.getSize() << endl; } int main() { // test01(); test02(); system("pause"); return 0; }
有参构造 arr1的打印输出为: abc 0 abc 1 abc 2 abc 3 abc 4 容量:5 大小:5 容量:5 大小:4 析构函数