C++之模板

目录

前言

一、模板

1.1 模板的概念

1.2 函数模板

二、模板案例1

案例描述:

三、普通函数与函数模板的区别

四、普通函数与函数模板的调用规则

五、模板具有局限性 

六、类模板的作用

七、类模板中成员函数创建时机

八、类模板对象做函数参数

九、类模板与继承

十、 类模板成员函数类外实现

十一、类模板分文件编写

十二、类模板与友元

十三、类模板案例


前言

C++泛型编程和STL技术详细讲解。

一、模板

1.1 模板的概念

模板就是建立通用模具大大提高复用性。

1.2 函数模板

C++另一种编程思想称为泛型编程,主要利用的技术就是模板。

C++提供两种模板机制:函数模板和类模板

1. 函数模板作用:建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个虚拟的类型来代表。

语法:

1.  template<typename T>

2.  函数声明或定义

template --- 声明创建模板

typename --- 表明其后面的符号是一种数据类型,可以用class代替。

T ---通用的数据类型,名称可以替换,通常为大写字母 。

#include<iostream>
using namespace std;
//函数模板
template<typename T>
void Swap(T& a,T& b)//引用传入,能改变实参
{
	T temp = a;
	a = b;
	b = temp;
}
int main()
{
	int a = 10;
	int b = 20;
	Swap(a, b);//自动类型推导。还有方法二:显示指定类型Swap<int>(a,b)
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	system("pause");
	return 0;
}

 注意事项:

● 自动类型推导,必须推导出一致的数据类型 T,才可以使用。

● 模板必须要确定出T的数据类型,才可以使用 T 。

二、模板案例1

案例描述:

1. 利用函数模板封装一个排序的函数,可以对不同数据类型数组进行排序

2. 排序规则从大到小,排序算法为选择排序

3. 分别利用 char 数组和 int 数组进行测试

#include<iostream>
using namespace std;
//交换模板
template<typename T>
void Swap(T& a,T& b)//引用传入,能改变实参
{
	T temp = a;
	a = b;
	b = temp;
}
//排序(从大到小)
template<typename T>
void Sort(T arr[], int len)//引用传入,能改变实参
{
	for (int i = 0; i < len; i++)
	{
		int max = i;//假设其为最大值下标
		for (int j = i + 1; j < len; j++)
		{
			if (arr[j] > arr[max])
			{
				max = j;
			}
		}
		if (max != i)
		{
			Swap(arr[max], arr[i]);
		}
	}
}
void test01()
{
	//测试char数组
	char chararr[] = "asjkfhwjk";
	int len = sizeof(chararr) / sizeof(chararr[1]);
	Sort(chararr, len);
	for (int i = 0; i < len; i++)
	{
		cout << chararr[i] << " ";
	}
	cout << endl;
}
void test02()
{
	//测试int数组
	int intarr[] = { 5,6,8,7,0,1,10};
	int len = sizeof(intarr) / sizeof(intarr[1]);
	Sort(intarr, len);
	for (int i = 0; i < len; i++)
	{
		cout << intarr[i] << " ";
	}
	cout << endl;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

三、普通函数与函数模板的区别

 普通函数与函数模板区别:

1. 普通函数调用时可以发生自动类型转换(隐式类型转换)。

2. 函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换。

3. 如果利用显示指定类型的方式,可以发生隐式类型转换。

四、普通函数与函数模板的调用规则

调用规则如下:

1. 如果函数模板和普通函数都可以实现,优先调用普通函数。

2. 可通过空模板参数列表来强制调用函数模板。

3. 函数模板也可以发生重载。

4. 如果函数模板可以产生更好的匹配,优先调用函数模板

#include<iostream>
using namespace std;
//1. 如果函数模板和普通函数都可以实现,优先调用普通函数。
//2. 可通过空模板参数列表来强制调用函数模板。
//3. 函数模板也可以发生重载。
//4. 如果函数模板可以产生更好的匹配, 优先调用函数模板。
//普通函数
void print(int a, int b)
{
	cout << "调用普通函数" << endl;
}
//函数模板
template<typename T>
void print(T a, T b)
{
	cout << "调用函数模板" << endl;
}
//函数模板的重载
template<typename T>
void print(T a, T b,T c)
{
	cout << "调用重载函数模板" << endl;
}
void test01()
{
	int a = 10;
	int b = 20;
	int c = 20;
	//优先调用普通函数
	print(a, b);
	//通过空模板参数列表,强制调用函数模板
	print<>(a, b);
	//调用函数模板的重载
	print(a, b,c);
	//如果函数模板可以产生更好的匹配, 优先调用函数模板
	char d = 'd';
	char e = 'e';
	print(d, e);//优先选择可以匹配的,这里可以匹配函数模板
}
int main()
{
	test01();
	system("pause");
	return 0;
}

五、模板具有局限性 

        下面案例中模板无法识别 person 类,这里需要利用具体化person的版本实现代码,具体化优先调用。在类前面加 template<>

#include<iostream>
using namespace std;
#include<string>
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 Compare(T &a, T &b)
{
	if (a == b)
	{
		return true;
	}
	else
	{
		return false;
	}
}
//利用具体化person的版本实现代码,具体化优先调用
template<>bool Compare(person & p1, person & p2)
{
	if(p1.m_name==p2.m_name&&p1.m_age==p2.m_age)
	{
		return true;
	}
	else
	{
		return false;
	}
}
void test01()
{
	int a = 10;
	int b = 20;
	bool ret = Compare(a, b);
	if (ret)
	{
		cout << "a=b" << endl;
	}
	else
	{
		cout << "a!=b" << endl;
	}
}
void test02()
{
	person p1("Tom", 10);
	person p2("Tom", 10);
	bool ret = Compare(p1, p2);
	if (ret)
	{
		cout << "p1=p2" << endl;
	}
	else
	{
		cout << "p1!=p2" << endl;
	}
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

小结:

1.利用具体化的模板,可以解决自定义类型的通用化。

2. 学习模板并不是为了写模板,而是在STL能够运用系统提供的模板

六、类模板的作用

建立一个通用类,类中的成员数据类型可以不具体制定,用一个虚拟的类型来代表。

语法与函数相同,在 template<typename T> 后面紧跟 类。

#include<iostream>
using namespace std;
#include<string>
//类模板
template<class name_type,class age_type>//自定义了名字和年龄的类型
class person
{
public:
	person(name_type name, age_type age)
	{
		this->m_name = name;
		this->m_age = age;
	}
	void show()
	{
		cout << "name:" << this->m_name << endl;
		cout << "age:" << m_age << endl;
	}
	name_type m_name;
	age_type m_age;
};
void test01()
{
	//类型参数化
	person<string, int>p1("张三", 19);
	p1.show();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

模板与函数模板区别主要有两点:

1. 类模板没有自动类型推导的使用方式。

必须在后面的对象中指明类型。(在类的后面加“< >”在里面说明类型)

2. 类模板在模板参数列表中可以有默认参数。

可以在参数列表: template<class name_type,class age_type = int >  这样设置默认参数类型,这样后面创建对象的时候就可以不说明类型了

七、类模板中成员函数创建时机

类模板中成员函数和普通类中成员函数创建时机是有区别的:

1. 普通类中的成员函数一开始就可以创建。

2. 类模板中的成员函数在调用时才创建。

#include<iostream>
using namespace std;
#include<string>
//类模板中成员函数的创建时机
//1. 普通类中的成员函数一开始就可以创建。
//2. 类模板中的成员函数在调用时才创建。
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 fun1()
		{
			obj.showperson1();
		}
		void fun2()
		{
			obj.showperson2();
		}
};
void test01()//类模板中的成员函数在调用时才会创建
{
	myclass<person1>m1;
	m1.fun1();
	myclass<person2>m2;
	m2.fun2();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

总结:类模板中的成员函数在调用时才会创建 

八、类模板对象做函数参数

共有三种传入方式:

1. 指定传入的类型——直接显示对象的数据类型

2. 参数模板化——将对象中的参数变为模板进行传递

3. 整个类模板化——将这个对象类型模板化进行传递

#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;
};

//1. 指定传入的类型——直接显示对象的数据类型
void printperson1(person<string, int> & p1)
{
	p1.showperson();
}
void test01()
{
	person<string, int>p1("张三", 20);
	printperson1(p1);
}

//2. 参数模板化——将对象中的参数变为模板进行传递
template<class T1,class T2>
void printperson2(person<T1, T2>&p2)
{
	p2.showperson();
}
void test02()
{
	person<string, int>p2("李四", 22);
	printperson2(p2);
}

//3. 整个类模板化——将这个对象类型模板化进行传递
template<class T>
void printperson3(T &p3)
{
	p3.showperson();
}
void test03()
{
	person<string, int>p3("王五", 23);
	printperson3(p3);
}

int main()
{
	test01();
	test02();
	test03();
	system("pause");
	return 0;
}

一般用第一种。 

九、类模板与继承

类模板碰到继承时,需要注意一下几点:

1. 当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T的类型

2. 如果不指定,编译器无法给子类分配内存

3. 如果想灵活指定出父类中T的类型,子类也需变为类模板

#include<iostream>
using namespace std;
#include<string>
//类模板与继承
template<class T>
class base
{
public:
	T m_1;
};
//如果想灵活指定出父类中T的类型,子类也需变为类模板
template<class T1, class T2>
class son :public base<T1>//子类在声明的时候,要指定出父类中T的类型
{
public:
	son()
	{
		cout << "T1的类型为:" << typeid(T1).name() << endl;
		cout << "T2的类型为:" << typeid(T2).name() << endl;
	}
	T2 m_2;
};
void test02()
{
	son<int, char>s;
}
int main()
{
	test02();
	system("pause");
	return 0;
}

 typeid().name()  这个函数可以用于查看参数类型。

上述代码运行结果如下图所示:

十、 类模板成员函数类外实现

        在类内声明,在类外写实现。首先需要加上类作用域,此外还需要在类(本案例中为person)后面声明参数类型(本案例中为template<class T1,class T2>),上面加一个模板。

#include<iostream>
using namespace std;
#include<string>
//类模板成员函数类外实现
template<class T1,class T2>
class person
{
public:
	person(T1 name, T2 age);
	void showperosn();
	T1 m_name;
	T2 m_age;
};
//构造函数的类外实现
template<class T1, class T2>
person<T1, T2>::person(T1 name, T2 age)
{
	m_name = name;
	m_age = age;
}
//成员函数的类外实现
template<class T1, class T2>
void person<T1, T2>::showperosn()
{
	cout << "姓名:" << m_name << "  年龄:" << m_age << endl;
}
void test01()
{
	person<string, int>p1("张三", 25);
	p1.showperosn();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

总结: 类模板中成员函数类外实现时,需要加上模板参数列表 。

十一、类模板分文件编写

问题 :

类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到

解决方法:

解决方式1: 直接包含.cpp源文件。

解决方式2: 将声明(.h 文件)和实现(.cpp 文件)写到同一个文件中,并更改后缀名为.hpp, hpp是约定的名称,并不是强制。

十二、类模板与友元

全局函数类内实现:直接在类内声明友元即可。

全局函数类外实现:需要提前让编译器知道全局函数的存在。

#include<iostream>
using namespace std;
#include<string>
//通过全局函数,打印person信息
template<class T1,class T2>
class person
{
	//全局函数 类内实现
	friend void printperson(person<T1,T2> p)
	{
		cout << "姓名:" << p.m_name << "   年龄:" << p.m_age << endl;
	}
	//全局函数 类外实现
	//需要在声明前加模板
	template<class T1, class T2>
	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;
};
//类外实现(全局函数。不需要加作用域)
template<class T1, class T2>
void printperson2(person<T1, T2> p)
{
	cout << "类外实现姓名:" << p.m_name << "   类外实现年龄:" << p.m_age << endl;
}
void test01()
{
	person<string, int> p("李四", 28);
	printperson(p);
}
void test02()
{
	person<string, int> p("王五", 25);
	printperson2(p);
}
int main()
{
	test01();
	test02();
	system("pasue");
	return 0;
}


十三、类模板案例

案例描述: 实现一个通用的数组类,要求如下:

1. 可以对内置数据类型以及自定义数据类型的数据进行存储

2. 将数组中的数据存储到堆区

3. 构造函数中可以传入数组的容量

4. 提供对应的拷贝构造函数以及operator=防止浅拷贝问题

5. 提供尾插法和尾删法对数组中的数据进行增加和删除

6. 可以通过下标的方式访问数组中的元素

7. 可以获取数组中当前元素个数和数组的容量

下面我建立了一个13case.hpp文件用于写模板的声明与实现

#pragma once
//自己的通用数组类
#include<iostream>
using namespace std;
#include<string>
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 = new T[arr.m_capacity];
		//将arr中的数据都拷贝过来
		for (int i = 0; i < this->m_size; i++)
		{
			this->paddress[i] = arr.paddress[i];
		}
	}
	//operator = 防止浅拷贝问题
	myarray& operator=(const myarray& arr)
	{
		cout << "myarray的operator = 函数调用" << endl;
		//先判断原来堆区是否有数据,如果有先释放
		if (this->paddress != NULL)
		{
			delete[]this->paddress;
			this->paddress = NULL;
			this->m_size = 0;
			this->m_capacity = 0;
		}
		//深拷贝
		this->m_capacity = arr.m_capacity;
		this->m_size = arr.m_size;
		this->paddress = new T[arr.m_capacity];
		//将arr中的数据都拷贝过来
		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[m_size] = val;//在数组末尾插入数据
		this->m_size++;//更新数组大小
	}
	//尾删法
	void pop_back()
	{
		//让用户访问不到最后一共元素,即为尾删,逻辑删除
		if (this->m_size == 0)
		{
			cout << "数组为空" << endl;
			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()
	{
		cout << "myarray的析构函数调用" << endl;
		if (this->paddress != NULL)
		{
			delete[]this->paddress;
			this->paddress = NULL;
		}
	}
private:
	T * paddress;//指针指向堆区开辟的真实的数组
	int m_capacity;//数组容量
	int m_size;//数组大小
};

在  “13.类模板案例.cpp”  文件中进行测试 。其中test01()为 int 类型的数组,test02()为 自定义数组,这里我自己定义了 person 类, 这里的myarray类模板依旧可以实现测试。

#include<iostream>
using namespace std;
#include "13case.hpp"
void test01()
{
	myarray<int> arr(5);
	for (int i = 0; i < 5; i++)
	{
		//利用尾插法向数组中插入数据
		arr.push_back(i);
	}
	cout << "arr的打印输出为:" << endl;
	for (int i = 0; i < arr.getsize(); i++)
	{
		cout << arr[i] << endl;
	}
	cout << "arr的容量为:"<<arr.getcapacity() << endl;
	cout << "arr的大小为:" << arr.getsize() << endl;
	myarray<int>arr2(arr);
	cout << "arr2的打印输出为:" << endl;
	for (int i = 0; i < arr2.getsize(); i++)
	{
		cout << arr2[i] << endl;
	}
	//尾删
	arr2.pop_back();
	cout << "arr尾删后:" << endl;
	cout << "arr2的容量为:" << arr2.getcapacity() << endl;
	cout << "arr2的大小为:" << arr2.getsize() << endl;
}
//测试自定义数据类型
class person
{
public:
	person() {};//后面写了有参构造函数就一定要加默认构造函数
	person(string name,int age)
	{
		m_name = name;
		m_age = age;
	}
	string m_name;
	int m_age;
};
void test02()
{
	myarray<person>arr(10);
	person p1("张三", 20);
	person p2("李四", 22);
	person p3("王五", 23);
	person p4("赵六", 32);
	person p5("吴七", 41);
	//将数据插入到数组中
	arr.push_back(p1);
	arr.push_back(p2);
	arr.push_back(p3);
	arr.push_back(p4);
	arr.push_back(p5);
	//打印数组
	for (int i = 0; i < arr.getsize(); i++)
	{
		cout << "姓名:" <<arr[i].m_name<< "  年龄:" << arr[i].m_age << endl;
	}
	//打印容量和大小
	cout << "arr的容量为:" << arr.getcapacity() << endl;
	cout << "arr的大小为:" << arr.getsize() << endl;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

拷贝构造函数和 operator= 运算符重载都是为了防止浅拷贝。


总结

总之,模板就是建立通用模具大大提高代码的复用性。

  • 11
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值