1函数模板基本使用
1.1template < class / typename T> 告诉编译器紧跟的代码里出现T不要报错
1.2mySwap( T &a T &b ) 类型也需要传入 ,类型参数化
1.3myswap(a,b) 自动类型推导 按照a b的类型 来替换T
1.4myswap(a,b) 显示指定类型
01 函数模板的基本用法.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//交换int类型两个数字
void mySwapInt( int & a, int & b)
{
int tmp = a;
a = b;
b = tmp;
}
//交换double数据
void mySwapDouble(double &a, double &b)
{
double tmp = a;
a = b;
b = tmp;
}
//类型,逻辑又非常相似
//类型参数化 泛型编程 -- 模板技术
template<class T> // 告诉编译器 下面如果出现T不要报错,T是一个通用的类型
void mySwap(T &a, T &b)
{
T tmp = a;
a = b;
b = tmp;
}
// template<typename T> 等价于 template<class T>
template<typename T>
void mySwap2(){}
void test01()
{
int a = 10;
int b = 20;
char c1 = 'c';
// mySwapInt(a, b);
//1 自动类型推导,必须有参数类型才可以推导
//mySwap(a, c1); 推导不出来T,所以不能运行
mySwap(a, b);
//2 显示指定类型
mySwap<int>(a, b);
//模板必须要指定出T才可以使用
mySwap2<double>();
cout << "a = " << a << endl;
cout << "b = " << b << endl;
double c = 3.14;
double d = 1.1;
//mySwapDouble(c, d);
mySwap(c, d);
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
2函数模板与普通函数的区别以及调用规则
2.1区别 普通函数可以进行隐式类型转换 模板不可以
2.2调用规则
2.2.1c++编译器优先考虑普通函数
2.2.2可以通过空模板实参列表的语法限定编译器只能通过模板匹配
2.2.3函数模板可以像普通函数那样可以被重载
2.2.4如果函数模板可以产生一个更好的匹配,那么选择模板
03 函数模板与普通函数的区别以及调用规则.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//1 普通函数与函数模板区别
template<class T>
T myPlus(T a, T b)
{
return a + b;
}
int myPlus2(int a, int b)
{
return a + b;
}
void test01()
{
int a = 10;
int b = 20;
char c = 'c'; // a = 97
// myPlus(a, c); //类型推导不出来 ,函数模板不可以进行隐式类型转换
cout << myPlus2(a, c) <<endl; // 10 + 99 普通函数 可以进行隐式类型转换
}
//2 、普通函数和函数模板的调用规则
template<class T>
void myPrint(T a ,T b)
{
cout << "模板调用的myPrint" << endl;
}
template<class T>
void myPrint(T a, T b ,T c)
{
cout << "模板调用的myPrint(a,b,c)" << endl;
}
通过模板生成的函数 叫模板函数
//void myPrint(int a, int b, int c)
//{
//
//}
void myPrint(int a, int b)
{
cout << "普通函数调用 myPrint" << endl;
}
void test02()
{
int a = 10;
int b = 20;
//1 、如果出现重载 优先使用普通函数调用,如果没有实现,出现错误
//myPrint(a, b);
//2、 如果想强制调用模板 ,那么可以使用空参数列表
myPrint<>(a, b);
//3、 函数模板可以发生重载
int c = 30;
myPrint(a, b, c);
//4、 如果函数模板可以产生更好的匹配,那么优先调用函数模板
char c1 = 'c';
char d = 'd';
myPrint(c1, d);
}
int main(){
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
2.3模板的机制
2.3.1模板并不是万能的,不能通用所有的数据类型
2.3.2模板不能直接调用,生成后的模板函数才可以调用
2.3.3二次编译,第一次对模板进行编译,第二次对替换T类型后的代码进行二次编译
02 课程练习-利用模板实现char和int的数组排序.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//对char和int数组进行排序 排序规则 从大到小 利用选择排序
template <class T>
void mySwap( T &a, T &b)
{
T tmp = a;
a = b;
b = tmp;
}
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++)
{
if (arr[max] < arr[j])
{
//交换 下标
max = j;
}
}
if (max != i)
{
//交换数据
mySwap(arr[max], arr[i]);
}
}
}
//输出数组元素的模板
template<class T>
void printArray( T arr[], int len)
{
for (int i = 0; i < len;i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
void test01()
{
char charArr[] = "helloworld";
int num = sizeof(charArr) / sizeof(char);
mySort(charArr, num);
printArray(charArr, num);
int intArr[] = { 1, 4, 100, 34, 55 };
int num2 = sizeof(intArr) / sizeof (int);
mySort(intArr, num2);
printArray(intArr, num2);
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
3模板局限性
3.1模板不能解决所有的类型
3.2如果出现不能解决的类型,可以通过第三地具体化来解决问题
3.3template<> 返回值 函数名<具体类型>(参数)
04 模板的局限性以及解决.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <string>
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<class T>
bool myCompare( T &a , T &b )
{
if ( a == b)
{
return true;
}
return false;
}
// 通过第三代具体化自定义数据类型,解决上述问题
// 如果具体化能够优先匹配,那么就选择具体化
// 语法 template<> 返回值 函数名<具体类型>(参数)
template<> bool myCompare<Person>(Person &a, Person &b)
{
if ( a.m_Age == b.m_Age)
{
return true;
}
return false;
}
void test01()
{
int a = 10;
int b = 20;
int ret = myCompare(a, b);
cout << "ret = " << ret << endl;
Person p1("Tom", 10);
Person p2("Jerry", 10);
int ret2 = myCompare(p1, p2);
cout << "ret2 = " << ret2 << endl;
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
4类模板
4.1写法template <T…> 紧跟着是类
4.2与函数模板区别,可以有默认类型参数
4.3函数模板可以进行自动类型推导,而类模板不可以
4.4类模板中的成员函数 一开始不会创建出来,而是在运行时才去创建
05 类模板的基本使用.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <string>
using namespace std;
//类模板
template <class NameType, class AgeType = int> //类模板可以有默认类型
class Person
{
public:
Person(NameType name, AgeType age)
{
this->m_Name = name;
this->m_Age = age;
}
void showPerson()
{
cout << "姓名:" << this->m_Name << " 年龄: " << this->m_Age << endl;
}
NameType m_Name;
AgeType m_Age;
};
void test01()
{
//自动类型推导 ,类模板 不支持
//Person p("孙悟空", 100);
//显示指定类型
Person<string, int> p("孙悟空", 100);
p.showPerson();
}
class Person1
{
public:
void showPerson1()
{
cout << "Person1的调用" << endl;
}
};
class Person2
{
public:
void showPerson2()
{
cout << "Person2的调用" << endl;
}
};
template<class T>
class myClass
{
public:
T obj;
void func1()
{
obj.showPerson1();
}
void func2()
{
obj.showPerson2();
}
};
//类模板中成员函数 一开始不会创建出来,而是在运行时才去创建
void test02()
{
myClass<Person1>m;
m.func1();
//m.func2();
}
int main(){
// test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
5类模板做函数的参数
5.1三种方式
5.1.1显示指定类型
5.1.2参数模板化
5.1.3整体模板化
5.2查看类型名称
5.2.1cout << typeid(T).name() << endl;
06 类模板做函数的参数.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <string>
using namespace std;
//类模板
template <class NameType, class AgeType = int> //类模板可以有默认类型
class Person
{
public:
Person(NameType name, AgeType age)
{
this->m_Name = name;
this->m_Age = age;
}
void showPerson()
{
cout << "姓名:" << this->m_Name << " 年龄: " << this->m_Age << endl;
}
NameType m_Name;
AgeType m_Age;
};
//1 指定传入类型
void doWork( Person<string ,int> & p )
{
p.showPerson();
}
void test01()
{
Person <string, int> p("MT",10);
doWork(p);
}
//2 参数模板化
template<class T1 ,class T2>
void doWork2(Person<T1, T2> & p)
{
//如何查看类型
cout << typeid(T1).name() << endl;
cout << typeid(T2).name() << endl;
p.showPerson();
}
void test02()
{
Person <string, int> p("呆贼", 18);
doWork2(p);
}
//3 整体模板化
template<class T>
void doWork3(T&p)
{
cout << typeid(T).name() << endl;
p.showPerson();
}
void test03()
{
Person <string, int> p("劣人", 18);
doWork3(p);
}
int main(){
// test01();
test02();
test03();
system("pause");
return EXIT_SUCCESS;
}
6当类模板碰到继承
6.1基类如果是模板类,必须让子类告诉编译器 基类中的T到底是什么类型
6.2如果不告诉,那么无法分配内存,编译不过
6.3利用参数列表class Child :public Base
07 类模板碰到继承的问题.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
template <class T>
class Base
{
public:
T m_A; //double类型
};
//child继承与 base必须告诉base中的T的类型,否则T无法分配内存
class Child :public Base<int>
{
};
//child2 也是模板类
template<class T1, class T2>
class Child2 :public Base<T2>
{
public:
Child2()
{
cout << typeid(T1).name() << endl;
cout << typeid(T2).name() << endl;
}
public:
T1 m_B; //int类型
};
void test01()
{
Child2<int, double>child;//由用户指定类型
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
7类模板的类外实现成员函数
template <class T1, class T2>
7.1Person<T1, T2>::Person(T1 name, T2 age)
08 类模板类外实现成员函数.cpp
#define _CRT_SECURE_NO_WARNINGS
#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 << "姓名:" << 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;
}
void test01()
{
Person <string ,int> p1("Mt", 100);
p1.showPerson();
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
8类模板的分文件编写问题以及解决
8.1.h .cpp分别写声明和实现
8.2但是由于 类模板的成员函数运行阶段才去创建,导致包含.h头文件,不会创建函数的实现,无法解析外部命令
8.3解决方案 保护 .cpp文件 (不推荐)
8.4不要进行分文件编写,写到同一个文件中,进行声明和实现,后缀名改为.hpp
8.5约定俗成的
Person.hpp
#pragma once
#include <iostream>
#include <string>
using namespace std;
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;
}
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;
//}
09 类模板的分文件编写问题以及解决.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <string>
using namespace std;
#include "Person.hpp"
//建议 模板不要做分文件编写,写到一个类中即可,类内进行声明和实现,最后把后缀名改为.hpp 约定俗成
int main(){
Person<string, int>p("猪八戒", 10);
p.showPerson();
system("pause");
return EXIT_SUCCESS;
}
9类模板碰到友元函数
9.1友元函数类内实现
9.2friend void printPerson( Person<T1 ,T2> & p )
9.3友元函数类外实现
9.4friend void printPerson<>(Person<T1, T2> & p); //没有<>普通函数 声明 加上 <>模板函数声明
9.5让编译器看到 函数 并且看到这个Person类型
10 类模板碰到了友元函数-类内实现.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <string>
using namespace std;
template<class T1 ,class T2>
class Person
{
//友元函数类内实现
friend void printPerson( Person<T1 ,T2> & p )
{
cout << "姓名:" << p.m_Name << " 年龄: " << p.m_Age << endl;
}
public:
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", 10);
printPerson(p);
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
11 类模板碰到友元函数-类外实现(1).cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <string>
using namespace std;
//让编译器提前看到printPerson声明
//让编译器看到Person类声明
template<class T1, class T2> class Person;
template<class T1, class T2>void printPerson(Person<T1, T2> & p);
template<class T1, class T2>
class Person
{
//友元函数类内实现 利用空参数列表 告诉编译器 模板函数的声明
friend void printPerson<>(Person<T1, T2> & p); //普通函数 声明
/*{
cout << "姓名:" << p.m_Name << " 年龄: " << p.m_Age << endl;
}*/
public:
Person(T1 name, T2 age)
{
this->m_Name = name;
this->m_Age = age;
}
private:
T1 m_Name;
T2 m_Age;
};
//类外实现
template<class T1 ,class T2>
void printPerson(Person<T1, T2> & p)
{
cout << "姓名:" << p.m_Name << " 年龄: " << p.m_Age << endl;
}
void test01()
{
Person<string, int> p("Tom", 10);
printPerson(p);
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
MyArray.hpp
#pragma once
#include <iostream>
using namespace std;
template< class T>
class MyArray
{
public:
//构造
explicit MyArray(int capacity) //防止隐式类型转换 防止MyArray arr = 10; 写法
{
this->m_Capacity = capacity;
this->m_Size = 0;
this->pAddress = new T[this->m_Capacity];
}
MyArray(const MyArray & array)
{
this->m_Capacity = array.m_Capacity;
this->m_Size = array.m_Size;
this->pAddress = new T[this->m_Capacity];
for (int i = 0; i < m_Size;i++)
{
this->pAddress[i] = array[i];
}
}
~MyArray()
{
if (this->pAddress != NULL)
{
delete[] this->pAddress;
this->pAddress = NULL;
}
}
//赋值操作符重载
MyArray& operator=(MyArray & array)
{
//先判断原始数据,有就清空
if (this->pAddress != NULL)
{
delete[] this->pAddress;
this->pAddress = NULL;
}
this->m_Capacity = array.m_Capacity;
this->m_Size = array.m_Size;
this->pAddress = new T[this->m_Capacity];
for (int i = 0; i < m_Size; i++)
{
this->pAddress[i] = array[i];
}
}
//[]重载
//MyArray arr(10);
//arr[0] = 100;
T & operator[]( int index)
{
return this->pAddress[index];
}
//尾插法
void push_Back( T val)
{
this->pAddress[this->m_Size] = val;
this->m_Size++;
}
//获取大小
int getSize()
{
return m_Size;
}
//获取容量
int getCapacity()
{
return this->m_Capacity;
}
private:
T * pAddress; //指向堆区指针
int m_Capacity; //容量
int m_Size;
};
12 类模板的应用-数组类封装.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include "MyArray.hpp"
#include <string>
//输出int类型数组
void printIntArray( MyArray<int>& array)
{
for (int i = 0; i < array.getSize();i++)
{
cout << array[i] << endl;
}
}
class Person
{
public:
Person(){};
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
//输出Person类型数组
void printPersonArray( MyArray<Person> & array )
{
for (int i = 0; i < array.getSize(); i++)
{
cout << "姓名: " << array[i].m_Name << " 年龄: " << array[i].m_Age << endl;
}
}
int main(){
MyArray <int >arr(10);
for (int i = 0; i < 10;i++)
{
arr.push_Back(i + 100);
}
printIntArray(arr);
Person p1("MT", 10);
Person p2("呆贼", 12);
Person p3("傻馒", 14);
Person p4("劣人", 15);
MyArray<Person>arr2(10);
arr2.push_Back(p1);
arr2.push_Back(p2);
arr2.push_Back(p3);
arr2.push_Back(p4);
printPersonArray(arr2);
system("pause");
return EXIT_SUCCESS;
}