模板是针对C++泛型编程的重要部分,可以大大提高代码的复用性
模板的特点:
-
模板不可以直接使用,它只是一个框架
-
模板的通用并不是万能的
C++提供两种模板机制:函数模板和类模板
函数模板的用法:
template<typename T>
函数声明或定义
实现对默认数据类型int和char数组的排序:
#include <iostream>
using namespace std;
template<typename T>
void mySwap(T& num1, T& num2)
{
T temp = num1;
num1 = num2;
num2 = temp;
}
template<typename 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<typename T>
void myPrint(T arr, int len)
{
for (size_t i = 0; i < len; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
void test01()
{
char charArr[] = "badcef";
int len = sizeof(charArr) / sizeof(char);
mySort(charArr, len);
myPrint(charArr, len);
}
void test02()
{
int intArr[] = { 1,3,5,7,8,6,4,2 };
int len = sizeof(intArr) / sizeof(int);
mySort(intArr, len);
myPrint(intArr, len);
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
类模板作用:
-
建立一个通用类,类中的成员 数据类型可以不具体制定,用一个虚拟的类型来代表。
类模板语法:
template<typename T>
类
类模板与函数模板的主要区别:
-
类模板没有自动类型推导的使用方式
-
类模板在模板参数列表中可以有默认参数
类模板的分文件实现:
MyArr.hpp 将类模板的声明及实现放在同一个文件中
#pragma once
#include <iostream>
#include <string>
#include <malloc.h>
using namespace std;
template<typename T>
class MyArr
{
public:
MyArr(int capacity)
{
//cout << "有参构造函数" << endl;
this->m_Size = 0;
this->m_Capacity = capacity;
m_Arr = new T[this->m_Capacity];
}
MyArr(const MyArr& arr)
{
//cout << "拷贝构造函数" << endl;
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
this->m_Arr = new T[this->m_Capacity];
for (int i = 0; i < this->m_Size; i++)
{
this->m_Arr[i] = arr.m_Arr[i];
}
}
~MyArr()
{
if (this->m_Arr != NULL)
{
//cout << "析构函数" << endl;
delete[] this->m_Arr;
this->m_Arr = NULL;
}
}
MyArr& operator=(const MyArr& arr)
{
cout << "operator=函数" << endl;
if (this->m_Arr != NULL)
{
delete[] this->m_Arr;
this->m_Capacity = 0;
this->m_Size = 0;
}
this->m_Arr = new T[arr.m_Capacity];
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
for (int i = 0; i < arr.m_Size; i++)
{
this->m_Arr[i] = arr.m_Arr[i];
}
return *this;
}
T& operator[](int pos)
{
return this->m_Arr[pos];
}
//尾插,失败返回-1,成功返回插入位置下标
int insertTail(const T& val)
{
if (this->m_Size >= this->m_Capacity)
{
cout << "Full" << endl;
return -1;
}
this->m_Arr[this->m_Size] = val;
this->m_Size++;
return this->m_Size;
}
//尾删,失败返回-1,成功返回0
int deleteTail()
{
if (this->m_Size == 0)
{
cout << "Empty" << endl;
return - 1;
}
this->m_Size--;
return 0;
}
int showSize()
{
return this->m_Size;
}
int showCapacity()
{
return this->m_Capacity;
}
private:
T* m_Arr;
int m_Size;
int m_Capacity;
};
MyArr.cpp 测试函数
#include "MyArr.hpp"
//自定义数据类
class Person
{
public:
Person() {}
Person(string name, int age);
string m_Name;
int m_Age;
};
Person::Person(string name, int age)
{
m_Name = name;
m_Age = age;
}
void printPersonArr(MyArr<Person>& arr)
{
for (int i = 0; i < arr.showSize(); i++)
{
cout << "Name:> " << arr[i].m_Name << "\tAge:> " << arr[i].m_Age << endl;
}
}
//自定义数据类型测试
void test01()
{
MyArr <Person>pArr(5);
Person p1("zhangsan", 18);
Person p2("lisi", 28);
Person p3("wangwu", 68);
pArr.insertTail(p1);
pArr.insertTail(p2);
pArr.insertTail(p3);
printPersonArr(pArr);
cout << "数据个数:> " << pArr.showSize() << endl;
cout << "数组容量:> " << pArr.showCapacity() << endl;
}
//默认数据类型测试
void test02()
{
MyArr <int>arr1(5);
for (int i = 0; i < 3; i++)
{
arr1.insertTail(i);
cout << arr1[i] << " ";
}
cout << endl;
cout << "数据个数:> " << arr1.showSize() << endl;
cout << "数组容量:> " << arr1.showCapacity() << endl;
}
int main()
{
test01();
//test02();
system("pause");
return 0;
}
注意:
-
类模板使用只能用显示指定类型方式
-
类模板中的模板参数列表可以有默认参数
-
普通类中的成员函数一开始就可以创建
-
类模板中的成员函数在调用时才创建