本阶段主要针对C++泛型编程和STL技术做详细的讲解,探讨C++更深层的使用
模板就是建立通用的模具,大大提高复用性
例如生活中的模板:
一寸照片模板:
模板的特点:
模板不可以直接使用,它只是一个框架
模板的通用并不是万能的
C++另一种编程思想称为泛型编程,主要利用的技术就是模板
C++提供了两种模板机制:函数模板和类模板
函数模板的使用:
建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个虚拟的类型来表示。
语法:
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()
{//使用函数模板 两种// 1.自动类型推导int a = 10;int b = 20;mySwap(a, b);
// 2.显示指定类型mySwap(a,b); //告诉编译器里的类型为intcout << "a=" << a << " b=" << b << endl;}
int main()
{test01();// int a = 10;// int b = 20;// swapInt(a, b);// cout << "a=" << a << " b=" << b << endl;// double c = 1.2;// double d = 2.2;// swapDouble(c, d);// cout << "c=" << c << " d=" << d << endl;return 0;
}
注意事项:
自动类型推导,必须推导出一致的数据类型T,才可以使用
模板必须要确定出T的数据类型,才可以使用
#include
using namespace std;//函数模板
template //函数模板可以用typename,可以用class
void mySwap(T& a, T& b)
{T temp = a;a = b;b = temp;
}template
void func()
{cout << "fun函数调用" << endl;
}void test01()
{//注意事项// 1. 自动类型推导,必须推导出一致的数据类型T,才可以使用int a = 10;int b = 20;// char c = 'c';// mySwap(a,c); //错误,推导不出一致的数据类型mySwap(a, b); //正确cout << "a=" << a << endl;cout << "b=" << b << endl;// 2. 模板必须要确定出T的数据类型,才可以使用// func(); //不确定函数的类型,不可以使用func();
}
int main()
{test01();// int a = 10;// int b = 20;// swapInt(a, b);// cout << "a=" << a << " b=" << b << endl;// double c = 1.2;// double d = 2.2;// swapDouble(c, d);// cout << "c=" << c << " d=" << d << endl;return 0;
}
总结:
使用函数模板时,必须确定出通用的数据类型T,并且能够推导出一致的类型
#include
using namespace std;//函数模板 案例
//实现通用的 对数据进行排序的函数
// 使用 选择排序
//测试 char int 数组//交换函数模板
template
void mySwap(T& a, T& b)
{T temp = a;a = b;b = temp;
}//打印数组模板
template
void printArray(T arr[], int len)
{for (int i = 0; i < len; i++) {cout << arr[i] << " ";}cout << endl;
}
//排序算法template
void myShort(T& array, int len)
{for (int i = 0; i < len; i++) {int max = i;for (int j = i + 1; j < len; j++) {if (array[j] > array[max]) {max = j;}}if (max != i) {//交换元素// int temp = array[i];// array[i] = array[max];// array[max] = temp;mySwap(array[max], array[i]);}}
}
void test01()
{char chararr[] = "bacde";myShort(chararr, sizeof(chararr) / sizeof(char));printArray(chararr, sizeof(chararr) / sizeof(char));cout << sizeof(chararr) / sizeof(char) << endl;//测试int 数组int array[5] = { 3, 12, 1, 4, 5 };myShort(array, sizeof(array) / sizeof(array[0]));printArray(array, sizeof(array) / sizeof(int));cout << sizeof(array) / sizeof(int) << endl;
}
int main()
{test01();return 0;
}
#include
using namespace std;//普通函数和函数模板的区别
// 1. 普通函数调用时可以发生 隐士类型转换
// 2. 函数模板 用自动类型推导 不会隐士类型转换
// 3. 函数模板 用显示指定类型,会发隐士类型转换
//普通函数
int myAdd01(int a, int b)
{return a + b;
}
template
int myAdd02(T a, T b)
{return a + b;
}void test01()
{int a = 10;int b = 20;char c = 'c'; // a - 97 c - 99cout << myAdd01(a, c) << endl; //可以调用,发送了隐式类型转换//使用模板调用// cout << myAdd02(a, c) << endl; //自动推导,没有隐式类型转换cout << myAdd02(a, c) << endl; //显示指定类型,有隐式类型转换
}
int main()
{test01();return 0;
}
#include
using namespace std;//普通函数和函数模板的调用规则
// 1.如果函数模板和普通函数都可以调用,优先调用普通函数
// 2.可以通过空模板参数列表强制调用函数模板
// 3.函数模板可以发生函数重载
// 4.如果函数模板可以产生更好的匹配,优先调用函数模板//普通函数
void myAdd01(int a, int b)
{cout << "调用普通函数" << endl;
}
template
void myAdd01(T a, T b)
{cout << "调用的函数模板" << endl;
}template
void myAdd01(T a, T b, T c)
{cout << "调用的重载的函数模板" << endl;
}void test01()
{int a = 10;int b = 20;int c = 30;// 1. 优先调用普通函数myAdd01(a, b);// 2.通过空模板的参数列表,强制调用函数模板myAdd01<>(a, b);myAdd01<>(a, b);// 3.函数模板可以发生函数重载myAdd01<>(a, b, c);// 4.如果函数模板可以产生更好的匹配,优先调用函数模板fchar c1 = 'a';char c2 = 'b';myAdd01(c1, c2);
}
int main()
{test01();return 0;
}
总结:既然提供了函数模板,最好就不要提供普通函数,否则容易出现二义性。
局限性:
模板的通用性并不是万能的
#include
using namespace std;
//模板的局限性
//模板并不是万能的,有些特点的数据类型,需要用具体的方式做特殊实现
class Person {
public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}string m_Name;int m_Age;
};
//对比两个数据是否相等函数
template
bool myCompare(T a, T b)
{if (a == b) {return true;} else {return false;}
}
//重载operator ==
bool operator==(Person a, Person b)
{if (a.m_Name == b.m_Name && a.m_Age == b.m_Age) {return true;} else {return false;}
}
//利用具体化的Person的版本实现代码,具体化优先调用
// template <>
// bool myCompare(Person a, Person b)
//{
// if (a.m_Name == b.m_Name && b.m_Age == b.m_Age) {
// return true;
// } else {
// return false;
// }
//}void test01()
{int a = 10;int b = 20;bool c = myCompare(a, b);if (c) {cout << "相等" << endl;} else {cout << "不相等" << endl;}
}void test02()
{Person a("Tom", 12);Person b("Tom", 12);bool c = myCompare(a, b); //报错,不能对比两个Person类,1.需要重载==运算符 2.在写一个重载if (c) {cout << "相等" << endl;} else {cout << "不相等" << endl;}}
int main()
{test02();return 0;
}
总结:
利用具体化的模板,可以解决自定义类型的通用化
学习模板并不是为了写模板,而是在STL能够运用系统提供的模板
类模板作用:
建立一个通用的类,类中的成员 数据类型可以不具体指定,用一个虚拟的类型来表示。
语法:
template
类
解释:
template --声明创建模板
typename -- 表明后面的符号是一种数据类型,可以用class代替
T -- 通用的数据类型,名称可以替换,通常为大写字母
#include
using namespace std;
//类模板
template
class Person {
public:Person(NameType name, AgeType age){m_Name = name;m_Age = age;}void showPerson(){cout << this->m_Name << " " << this->m_Age << endl;}NameType m_Name;AgeType m_Age;
};
void test01()
{Person p("张三", 2);p.showPerson();
}void test02()
{
}
int main()
{test01();return 0;
}
类模板与函数模板区别主要有两点:
1.类模板没有自动类型推导的使用方式
2.类模板在模板参数列表中可以有默认参数
#include
using namespace std;
//类模板和函数模板的区别
template // age默认类型整型
class Person {
public:NameType m_Name;AgeType m_Age;Person(NameType name, AgeType age){this->m_Name = name;this->m_Age = age;}void showPerson(){cout << this->m_Name << " " << this->m_Age << endl;}
};
void test01()
{// 1.类模板没有自动类型推导Person p("张三", 21); // C++17之后支持自动类型推导Person p1("张三", 2);p.showPerson();
}void test02()
{// 2.类模板在模板参数中可以有默认参数Person p("李四", 12);Person p1("李四", 13);p.showPerson();p1.showPerson();
}
int main()
{test02();return 0;
}
总结:
类模板只能用显示指定类型
类模板可以有默认参数
#include
using namespace std;
//类模板中成员函数调用时机//类模板成员函数和普通类中的成员函数创建时机是区别的
// 1.普通类中的成员函数一开始就可以创建
// 2类模板中的成员函数调用时创建.
class Person1 {
public:void show_Person1(){cout << "show_Person1" << endl;}
};
class Person2 {
public:void show_Person2(){cout << "show_Person2" << endl;}
};template
class myClass {
public:T obj;//类模板中的成员函数void func1(){obj.show_Person1();}void func2(){obj.show_Person2();}
};
void test01()
{//类模板在调用时,才会运行myClass m; //类模板需要写<>m.func1(); //可以调用
// m.func2();//不可以调用
}void test02()
{myClass m; //类模板需要写<>
// m.func1(); //不可以调用m.func2();//可以调用
}
int main()
{test02();return 0;
}
学习目标:
类模板实例化出的对象,向函数传参的方式
一共有三种方式:
1.指定传入类型 --直接显示对象的数据类型
2.参数模板化 --将对象中的参数变为模板进行传递
3.整个类模板化 --将这个对象类型 模板化进行传递
#include
using namespace std;
//类模板对象做函数参数
template
class Person {
public:T1 m_Name;T2 m_Age;Person(T1 name, T2 age){this->m_Name = name;this->m_Age = age;}void showPerson(){cout << "姓名:" << this->m_Name << endl;cout << "年龄:" << this->m_Age << endl;cout << "T1的类型为" << typeid(T1).name() << endl;cout << "T2的类型为" << typeid(T2).name() << endl;}
};
// 1.指定传入类型
void printPerson1(Person& p)
{p.showPerson();
}
// 2.参数模板化
template
void printPerson2(Person& p)
{p.showPerson();
}
// 3.整个类模板化
template
void printPerson3(T& p)
{p.showPerson();
}
void test01()
{Person p("张三", 18);printPerson1(p);
}void test02()
{Person p("李四", 18);printPerson2(p);
}
void test03()
{Person p("王五", 18);printPerson3(p);
}
int main()
{test03();//最常用第一种return 0;
}
总结:第一种最常用
#include
using namespace std;
//类模板与继承
template
class Base {T m;
};// class Son:public Base //错误,必须知道父类种T数据类型才可以继承给子类
class Son : public Base //知道类型就可以继承了
{
};template
class Son2 : public Base //知道类型就可以继承了
{
public:Son2(){cout << "t1的类型为:" << typeid(T1).name() << endl;cout << "t2的类型为:" << typeid(T2).name() << endl;}T1 obj;
};
void test01()
{Son s1; //可以实例化
}//如果想灵活的指定父类中的数据类型,需要变为子类模板
void test02()
{Son2 s2;
}int main()
{test02();return 0;
}
总结:如果父类是类模板,子类在继承时要指定父类的数据类型,或者类变为类模板。
学习目标:能够掌握类模板中的成员函数类外实现
#include
using namespace std;
//类模板中成员函数类外实现
template
class Person {
public:T1 m_Name;T2 m_Age;// Person(T1 name, T2 age)// {// this->m_Name = name;// this->m_Age = age;// }// void showPerson()// {// cout << "姓名" << this->m_Name << endl;// cout << "年龄" << this->m_Age << endl;// }//如果在类外实现的化需要,类内声明,类外实现Person(T1 name, T2 age);void showPerson();
};//构造函数类外实现
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;
}
void test01()
{Person p("张三", 14);p.showPerson();
}void test02()
{
}int main()
{test01();return 0;
}
//#include "person.h" //包含h会报错,因为包含头文件,头文件是模板运行才知道数据类型
//#include "person.cpp" //包含cpp不会报错
//第二种方法,将.h和.cpp文件写入一起,后缀名为.hpp文件,代表类模板
#include "person.hpp"#include
#include
using namespace std;//类模板分文件编写问题及解决
// template
// class Person {
// public:
// T1 m_Name;
// T2 m_Age;
// Person(T1 name, T2 age);
// void showPerson();
//};// template
// Person::Person(T1 name, T2 age)
//{
// this->m_Name = name;
// this->m_Age = age;
// }
// template
// void Person::showPerson()
//{
// cout << "年龄" << this->m_Age << endl;
// }void test01()
{//现在运行报错Person p("李四", 12);p.showPerson();
}
int main()
{test01();return 0;
}
person.hpp
#ifndef PERSON_H
#define PERSON_H
#pragma once
#include using namespace std;template
class Person {
public:T1 m_Name;T2 m_Age;Person(T1 name, T2 age);void showPerson();
};template
Person::Person(T1 name, T2 age)
{this->m_Name = name;this->m_Age = age;
}
template
void Person::showPerson()
{cout << "年龄" << this->m_Age << endl;
}#endif // PERSON_H
总结:主流的解决方式是第二种,写到一起,改名称为hpp
#include
#include
using namespace std;//先让编译器知道有模板和类的存在
template
class Person;// 2.全局函数,类外实现
//函数模板
template
void printPerson2(Person p)
{cout << "姓名" << p.m_Name << endl;cout << "年龄" << p.m_Age << endl;
}//通过全局函数,打印Person信息
template
class Person {private:T1 m_Name;T2 m_Age;private:// 1.全局函数,类内实现//在私有属性里加入friend 声明为类的友元//全局函数?不太懂//全局函数,似乎只要是友元,那么他就是全局函数friend void printPerson(Person p){cout << "姓名" << p.m_Name << endl;cout << "年龄" << p.m_Age << endl;}// 2.全局函数,类外实现//普通函数//加空模板参数列表//如果全局函数是类外实现,让编译器提前知道这个函数的存在friend void printPerson2<>(Person p);public:Person(T1 name, T2 age){this->m_Name = name;this->m_Age = age;}
};void test01()
{Person p("李四", 12);printPerson(p);
}
void test02()
{Person p("张三", 1);printPerson2(p);
}
int main()
{test02();return 0;
}