目录
函数模板
函数模板基本语法
#include<iostream>
using namespace std;
//函数模板作用,其函数返回值类型和形参类型可以不确定制定,用一个虚拟的类型来代表
//语法 template<typename T>函数声明或定义
//template 声明创建模板
//typename 表明其后面的符号是一种数据类型,可以用class代替
//T通用的数据类型,名称可以替换,通常为大写字母
//两个整形交换的函数
template<typename T>//声明一个模板,告诉编译器,后面代码中紧跟着的T不要报错,T是一个通用的数据类型
void mySwap(T& a, T& b)
{
T temp = a;
a = b;
b = temp;
}
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;
}
void test01()
{
int a = 10;
int b = 20;
/*swapInt(a, b);*/
//两种方式使用函数模板
//1.自动类型推到
/*mySwap(a, b);*/
//2.显示指定类型
mySwap<int>(a, b);
cout << "a = " << a << endl << "b = " << b << endl;
double c = 1.1;
double d = 2.2;
swapDouble(c, d);
cout << "c = " << c << endl << "d = " << d << endl;
}
//函数模板
int main()
{
test01();
system("pause");
return 0;
}
函数模板注意事项
#include<iostream>
using namespace std;
//函数模板注意事项
//1.自动类型推到,必须推导出一致的数据类新T才可以使用
template<class T>//typename 可以替换为 class 实际上没有区别
void mySwap(T& a, T& b)
{
T temp = a;
a = b;
b = temp;
}
void test01()
{
int a = 10;
int b = 20;
mySwap<int>(a, b);
cout << "a = " << a << endl << "b = " << b << endl;
}
//2,模板必须要确定出T的数据类型,才可以使用
template<class T>
void func()
{
cout << "func调用" << endl;
}
void test02()
{
/*func();*///这里发生错误
func<int>();
}
int main()
{
test01();
system("pause");
return 0;
}
函数模板的局限性
#include<iostream>
#include<string>
using namespace std;
//模板的通用性不是万能的 有些特性的数据类型,需要用具体化凡是做特殊实现
//对比两个数据是否相等的函数
class Person
{
public:
Person(string name, int age)
{
this->m_Age = age;
this->m_Name = name;
}
string m_Name;
int m_Age;
};
template<class T>
bool myCompare(T& a, T& b)
{
if (a == b)
{
return true;
}
return false;
}
//利用具体化Person的版本实现代码,具体化优先调用
template<>bool myCompare(Person& p1, Person& p2)
{
if (p1.m_Age == p2.m_Age && p1.m_Name == p2.m_Name)
{
return true;
}
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;
}
return;
}
void test02()
{
Person p1("HRY", 10);
Person p2("HRY", 10);
bool ret = myCompare(p1, p2);
if (ret)
{
cout << "p1 == p2" << endl;
}
else
{
cout << "p1 != 2" << endl;
}
return;
}
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;
cout << endl << "函数模板运算结果:" << endl;
//自动类型推导 不会发生隐式类型转换
cout << "a + b = " << myAdd01(a, b) << endl;
//cout << "a + 'c' = " << myAdd02(a, c) << endl;//myAdd02(a, c)是错误的
//显示指定类型 会发生隐式类型转换
cout << "a + 'c' = " << myAdd02<int>(a, c) << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
普通函数和函数模板的调用规则
#include<iostream>
using namespace std;
//如果普通函数和函数模板都可以调用,优先调用普通函数
//可以通过空模板的参数列表 强制调用函数模板
//函数模板可以发生函数重载
//如果函数模板可以产生更好的匹配,优先调用函数模板
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 = 20;
int c = 30;
myPrint(a, b);
//通过空模板的参数列表,强制调用函数模板
//myPrint(a, b,c);
//myPrint<int>(a, b,c);
char c1 = 'a';
char c2 = 'b';
myPrint(c1, c2);
}
int main()
{
test01();
system("pause");
return 0;
}
函数模板案例-数组排序
#include<iostream>
using namespace std;
//实现通用 对数组进行排序的函数
//规则 从大到小
//算法 选择排序
//测试 char int
//交换函数模板
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]);
}
}
}
//提供打印数组模板
template<class T>
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 intArr[] = { 2,34,5,6,72,5,6,2,56,42 };
int num = sizeof(charArr) / sizeof(char);
mySort(charArr,num );
mySort(intArr, sizeof(intArr) / sizeof(int));
printArray(charArr, num);
printArray(intArr, sizeof(intArr) / sizeof(int));
}
int main()
{
test01();
system("pause");
return 0;
}
类模板
类模板基本语法
#include<iostream>
#include<string>
using namespace std;
//类模板
template<class Nametype,class AgeType>
class Person
{
public:
Person(Nametype name, AgeType age)
{
this->m_Name = name;
this->m_Age = age;
}
Nametype m_Name;
AgeType m_Age;
};
void test01()
{
Person<string, int> p1("HRY", 123);
cout << "name:" << p1.m_Name<<" ";
cout << "age:" <<p1.m_Age<< endl;
}
int main()
{
test01();
system("pause");
return 0;
}
类模板和继承
#include<iostream>
using namespace std;
//当类模板碰到继承时,需要注意以下几点
//当子类继承的父类是一个类模板时,子类在声明的时候,要指定父类中的T的类型
//如果不指定,编译器无法给子类分配内存
//如果想灵活指定父类中T的类型,子类也需变为类模板
template<class T>
class Base
{
T m;
};
/*class Son :public Bas*///错误,必须知道父类中T的类型,才能继承给子类
class Son :public Base<int>
{
};
void test01()
{
Son 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()
{
test02();
system("pause");
return 0;
}
类模板与友元
#include<iostream>
#include<string>
using namespace std;
//掌握类模板配合友元函数的类内和类外实现
//全局函数类内实现 直接在类内声明友元即可
//全局函数类外实现 需要让编译器提前知道全局函数的存在
//通过全局函数 打印person信息
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;
}
template<class T1,class T2>
class person
{
//全局函数 类内实现
friend void printperson(person<T1,T2> p)
{
cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
}
//全局函数类外实现
//加空模板参数列表
//如果全局函数类外实现 需要让编译器提前知道这个函数存在
friend void printperson2<>(person<T1, T2> p);
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("HRY", 20);
printperson(p);
}
void test02()
{
person<string, int>p("HRY", 30);
printperson2(p);
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
类模板成员函数类外实现
#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;
//}
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>p("HRY", 20);
p.showPerson();
}
int main()
{
test01();
system("pause");
return 0;
}
类模板对象做函数参数
//类模板的对象做函数参数
#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 << "name:" << this->m_Name << " age:" << this->m_Age << endl;
}
T1 m_Name;
T2 m_Age;
};
void printPerson1(person<string, int>&p)
{
p.showPerson();
}
void test01()
{
person<string, int>p("HRY", 99);
printPerson1(p);
}
//参数模板化--------将对象中的参数变为模板进行传递
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;
}
void test02()
{
person<string, int>p("HRY", 999);
printPerson2(p);
}
//整个类模板化------将这个对象类型 模板化进行传递
template<class T>
void printPerson3(T& p)
{
p.showPerson();
cout << "T的数据类型:" << typeid(T).name() << endl;
}
void test03()
{
person<string, int>p("HRY", 9);
printPerson3(p);
}
int main()
{
test01();
test02();
test03();
system("pause");
return 0;
}
类模板分文件编写
类模板分文件编写
//类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到
//解决方式1 直接包含cpp文件
//解决方式2 将声明和实现写到同一个文件中,并更改后缀名为 .hpp hpp为约定的名称,并不是强制
//#include<iostream>
//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;
//}
//第一种解决方式 直接包含 源文件
#include"person.cpp"//必须包含这个
//第二种解决方式 将 .h 和 .cpp 中内容写在一起 后缀名改为 .hpp文件
#include"person.hpp"//
void test01()
{
person<string ,int> p("HRY",11);
p.showperson();
}
int main()
{
test01();
system("pause");
return 0;
}
person.hpp
#pragma once
#include<iostream>
using namespace std;
#include<iostream>
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;
}
类模板和函数模板的区别
#include<iostream>
#include<string>
using namespace std;
//类模板没有自动类型推导的使用方式
//类模板在模板参数列表中可以有默认参数
template<class NameType =string,class AgeType=int>//等于号后面的内容即默认参数,函数模板中无
class person
{
public:
person(NameType name, AgeType age)
{
this->m_Name = name;
this->n_Age = age;
}
void showPerson()
{
cout << "name:" << this->m_Name << " "
<< "age:" << this->n_Age << endl;
}
NameType m_Name;
AgeType n_Age;
};
//类模板没有自动类型推导的使用方式
void test01()
{
//person p1("HRY", 111); 这里错误
person<string,int> p1("HRY", 111);//只能用显示指定类型
p1.showPerson();
}
//类模板在模板参数列表中可以有默认参数
void test02()
{
person<> p2("hry", 222);
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;
}
类模板案例-数组类封装
/*可以以对内置数据类型以及自定义数据类型的数据进行存储
将数据中的数据存储到堆区
构造函数中可以传入数组的容量
提供对应的拷贝构造函数以及operator=防止浅拷贝问题
提供尾插法和尾删法对数组中的数据进行增加和删除
可以通过下标的方式访问数组中的元素
可以获取数组中当前元素个数和数组的容量
*/
#include"MyArray.hpp"
void printIntArray(MyArray<int>&arr)
{
for (int i = 0; i < arr.getSize(); i++)
{
cout << arr[i]<<" ";
}
}
void test01()
{
MyArray<int> arr1(5);
for (int i = 0; i < 5; i++)
{
arr1.Push_Back(i);
}
cout << "arr1的打印输出为:" << endl;
printIntArray(arr1);
//MyArray<int>arr2(arr1);
//MyArray<int>arr3(100);
//arr3 = arr1;
}
class person
{
public:
person() {};
person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void printPersonArray(MyArray<person>& personArr)
{
for (int i = 0; i < personArr.getSize(); i++)
{
cout << "name:" << personArr[i].m_Name << " age:" << personArr[i].m_Age;
}
}
void test02()
{
MyArray<person> p(1);
person p1("HRY", 11);
p.Push_Back(p1);
}
int main()
{
test01();
system("pause");
return 0;
}
MyArray.hpp
#pragma once
#include<iostream>
using namespace std;
template<class T>
class MyArray
{
public:
//有参构造
MyArray(int capacity)
{
cout << "MyArray的有参构造调用" << endl;
this->m_Capacity = capacity;
this->m_Size = 0;
this->pAddress = new T[this->m_Capacity];
}
//拷贝构造
MyArray(const MyArray& arr)
{
cout << "MyArray的拷贝构造调用" << endl;
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
//this->pAddress = arr.pAddress;
//深拷贝
this->pAddress = new T[arr.m_Capacity];
//将arr中的数据拷贝过来
for (int i = 0; i < this->m_Size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
}
MyArray& operator=(const MyArray& arr)
{
cout << "MyArray的operator=调用" << endl;
//先判断原来堆区是否有数据 如果有先释放
if (this->pAddress!= NULL)
{
delete[] this->pAddress;
this->pAddress = NULL;
this->m_Capacity = 0;
this->m_Size = 0;
}
//深拷贝
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
this->pAddress = new T[arr.m_Capacity];
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_Capacity == 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--;
}
//通过下标方式访问数组中元素
T& operator[](int index)
{
return this->pAddress[index];
}
//返回数组容量
int getCapacity()
{
return this->m_Capacity();
}
//返回数组大小
int getSize()
{
return this->m_Size();
}
//析构函数
~MyArray()
{
if(this->pAddress!=NULL)
{
cout << "MyArray的析构函数调用" << endl;
delete[]this->pAddress;
this->pAddress=NULL:
}
}
private:
T* pAddress;//指针指向堆区开辟的真实数组
int m_Capacity;//数组容量
int m_Size;//数组大小
};