浅谈C++|模板篇

2023-09-14 23:30:35

一.模板模板概念

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

 模板的特点:

1.模板不可以直接使用,它只是一个框架

2.模板的通用并不是万能的

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

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

 二.函数模板

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

2.1语法

template<typename T>
函数声明或定义

自动推导类型代码实例:

#include <iostream>
using namespace std;
template<typename T>
void my_swap(T& a, T& b) {
	T temp = a;
	a = b;
	b = temp;
}

int main() {
	int a = 10,b = 20;
	char a2 = 'a', b2 = 'b';
	string a3 = "abc", b3 = "efg";
	my_swap(a, b);
	my_swap(a2, b2);
	my_swap(a3, b3);
	cout << "a=" << a << " b=" << b << endl;
	cout << "a2=" << a2 << " b2=" << b2 << endl;
	cout << "a3=" << a3 << " b3=" << b3 << endl;
	return 0;
}

 显示指定类型代码:

#include <iostream>
using namespace std;
template<typename T>
void my_swap(T& a, T& b) {
	T temp = a;
	a = b;
	b = temp;
}

int main() {
	int a = 10,b = 20;
	char a2 = 'a', b2 = 'b';
	string a3 = "abc", b3 = "efg";
	my_swap<int>(a, b);
	my_swap<char>(a2, b2);
	my_swap<string>(a3, b3);
	cout << "a=" << a << " b=" << b << endl;
	cout << "a2=" << a2 << " b2=" << b2 << endl;
	cout << "a3=" << a3 << " b3=" << b3 << endl;
	return 0;
}

 2.2注意事项

注意事项:
1.自动类型推导,必须推导出一致的数据类型T,才可以使用

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

 代码实例:

#include <iostream>
using namespace std;
template<typename T>
void my_swap(T a, T B) {
	T temp = a;
	a = b;
	b = a;
}

template<typename T>
void fun() {
	cout << "函数模板调用" << endl;
}

int mian() {
	//fun();//报错,因为不能推理出T的类型
	int a = 10, b = 20;
	fun<int>();   
	//必须这样写,int换为其它类型也可
	

	//my_swap(10, 'p');
	//报错,类型不一致

	my_swap(20, 90);  
  
    my_swap<int>(20, 90);  
	my_swap(a, b);

	return 0;
}

2.3普通函数和模板函数的区别

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

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

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

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

#include <iostream>
using namespace std;
void fun(int one, int two) {
	cout << "one=" << one << " two=" << two << endl;
}
template<typename T>
void fun1(T one, T two) {
	cout << "one=" << one << " two=" << two << endl;
}


void ce() {
	int a = 10, b = 20;
	char c = 'a';
	fun(a, b);
	fun(a, c);
	fun1(a, b);
	//fun1(a, c);  
	//报错,因为自动推理类型不会发生隐式转换

	fun1<int>(a, c);

}
int main() {
	ce();
	return 0;
}

 总结:建议使用显示指定类型的方式,调用函数模板,因为可以自己确定通用类型T

 2.4普通函数和函数模板的调用规则

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

2.可以通过空模板参数列表来强制调用函数模板
3.函数模板也可以发生重载
4.如果函数模板可以产生更好的匹配,优先调用函数模板

代码实例: 

#include <iostream>
using namespace std;
void fun(int a,int b) {
	cout << a << ' ' << b << ' ' << endl;
	cout << "普通函数调用" << endl;
}

template<class T>
void fun(T a, T b) {
	cout << a << ' ' << b << ' ' << endl;
	cout << "模板函数调用" << endl;
}


template<class T>
void fun(T a, T b,T c) {
	cout << a << ' ' << b << ' ' <<c << endl;
	cout << "模板函数调用" << endl;
}
int main() {
	fun(10,20);   //默认调用普通函数
	fun<>(10,90);  //空列表强制调用模板函数
	fun<>(10,90,90);  //模板函数可以发生重载
	return 0;
}

 2.5模板的局限性

 局限性:

模板的通用性并不是万能的

 代码实例:

#include <iostream>
using namespace std;

class person {
public:
	person(string name, int temp) {
		this->name = name;
		this->temp = temp;
	}
	int temp;
	string name;
};


template <class T>
bool eq(T a, T b) {
	if (a == b) {
		return true;
	}
	else {
		return false;
	}
}

int main() {
	person p1("tom", 20);
	person p2("tom", 20);
	//报错,因为自定义的类型,重载‘=’运算符
	if (eq(p1, p2)) {
		cout << "=" << endl;
	}
	else {
		cout << "!=" << endl;
	}
	return 0;
}

此时模板函数不报错,但运行时会报错。因为类的‘==’没有重载。

 代码实例:

#include <iostream>
using namespace std;
class person {
public:
	person(string name, int temp) {
		this->name = name;
		this->temp = temp;
	}
	int temp;
	string name;
};
template <class T>
bool eq(T a, T b) {
	if (a == b) {
		return true;
	}
	else {
		return false;
	}
}

//实例化模板函数
template<> bool eq(person a, person b) {
	if (a.name == b.name && a.temp == b.temp) {
		return true;
	}
	else {
		return false;
	}
}


int main() {
	person p1("tom", 20);
	person p2("tom", 20);
	//报错,因为自定义的类型,重载‘=’运算符
	if (eq(p1, p2)) {
		cout << "=" << endl;
	}
	else {
		cout << "!=" << endl;
	}
	return 0;
}

此时可以运行,函数模板实例化,相当于模板的补充。

三.类模板

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

类模板不能使用自动推导类型,只能用显示指定的方法。

3.1语法 

template<typename T1, typename T2>
class person {
public:
	person(T1 name, T2 age) {
		this->name = name;
		this->age = age;
	}
	T1 name;
	T2 age;
};

代码实例:

#include <iostream>
using namespace std;
template<typename T1, typename T2>
class person {
public:
	person(T1 name, T2 age) {
		this->name = name;
		this->age = age;
	}
	T1 name;
	T2 age;
};


int main() {
	person<string, int> P("TOM", 90);
	cout << P.name << ' ' << P.age << endl;
	return 0;
}

 3.2类模板和函数模板区别

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

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

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

代码实例: 

#include <iostream>
using namespace std;
//类模板可以添加默认参数,但是要优先右边全有默认参数yuanze
template<class T1=string, typename T2=int>
class person {
public:
	person(T1 name, T2 age) {
		this->name = name;
		this->age = age;
	}
	T1 name;
	T2 age;
};
int main() {
	//person P("TOM", 90); //报错因为。类模板没有自动推导型
	person<string, int> P("TOM", 90);
	//有默认参数,可以省略类型,但是<>不能省略
	person<> P1("SOM", 900);
	cout << P.name << ' ' << P.age << endl;
	cout << P1.name << ' ' << P.age << endl;
	return 0;
}

注意:< >不可以省略,设置默认参数时,优先右边。

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

类模板中成员函数和普通类中成员函数创建时机是有区别的:
1.普通类中的成员函数—开始就可以创建
2.类模板中的成员函数在调用时才创建

代码实例:

#include <iostream>
using namespace std;
class person1 {
public:
	void fun1() {
		cout << "person1的fun函数调用" << endl;
	}
};
class person2 {
public:
	void fun2() {
		cout << "person2的fun函数调用" << endl;
	}
};

template<typename T>
class p {
public:
	p(T p) {
		this->fun = p;
	}
	T fun;
	void fun1() {
		//定义时不报错,只有实例化后,调用此函数才会检测是否报错。
		fun.fun1();
	}
	void fun2() {
		//定义时不报错,只有实例化后,调用此函数才会检测是否报错。
		fun.fun2();
	}
};


int main() {
	person1 p1;
	p<person1> p(p1);
	//此时会正常运行,不会报错,因为没有调用p.fun2();
	p.fun1();
	//p.fun2();   运行报错
	return 0;
}

3.4类模板做函数参数

—共有三种传入方式:
1.指定传入的类型------------直接显示对象的数据类型

⒉参数模板化------------------将对象中的参数变为模板进行传递
3.整个类模板化----------------将这个对象类型模板化进行传递

3.4.1指定传入

代码实例:

#include <iostream>
using namespace std;
//默认参数
template<class T1=string,class T2=int>
class person {
public:
	T1 name;
	T2 id;
	person(T1 name, T2 id) {
		this->name = name;
		this->id = id;
	}
	void fun() {
		cout << name << ' ' << id << endl;
	}
};

//此函数不是模板函数,所以其参数要具体的参数
void fun(person<string, int>& p) {
	p.fun();
}

int main() {
	person<string, int> p("孙悟空", 78);
	fun(p);
	return 0;
}

 3.4.2参数模板化

代码实例:

#include <iostream>
using namespace std;
//默认参数
template<class T1 = string, class T2 = int>
class person {
public:
	T1 name;
	T2 id;
	person(T1 name, T2 id) {
		this->name = name;
		this->id = id;
	}
	void fun() {
		cout << name << ' ' << id << endl;
	}
};

//实际上是创建模板函数
template<class T1,class T2>
void fun(person<T1, T2>& p) {
	p.fun();
}

int main() {
	person<string, int> p("猪八戒", 19);
	fun(p);
	return 0;
}

3.4.3类模板化

代码实例:

#include <iostream>
#include <string>
using namespace std;
//默认参数
template<class T1 = string, class T2 = int>
class person {
public:
	T1 name;
	T2 id;
	person(T1 name, T2 id) {
		this->name = name;
		this->id = id;
	}
	void fun() {
		cout << name << ' ' << id << endl;
	}
};

//将整个类作为一个类型,创建模板函数
template<class T1>
void fun(T1& p) {
	p.fun();
}

int main() {
	person<string, int> p("沙僧", 19);
	fun(p);
	return 0;
}

总结:
通过类模板创建的对象,可以有三种方式向函数中进行传参

使用比较广泛是第一种:指定传入的类型

 3.5类模板与继承

当类模板碰到继承时,需要注意一下几点:
1.当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T的类型,如果不指定,编译器无法给子类分配内存。
2.如果想灵活指定出父类中T的类型,子类也需变为类模板。

 1.代码实例:

#include <iostream>
using namespace std;
template<typename T>
class father {
public:
	T name;
};

//class father :public son 
//如果想灵活指定出父类中T的类型,子类也需变为类模板。
class son :public father<string> {
public:
	son(string name) {
		this->name = name;
	}
	void fun() {
		cout << this->name << endl;
	}
};

int main() {
	son s("Tom");
	s.fun();
	return 0;
}

 2.代码实例:

#include <iostream>
using namespace std;
template<typename T>
class father {
public:
	T name;
};

//class father :public son 
//父类是模板类时,子类继承时要指定模板列表
template<class T1,class T2>
class son :public father<T2> {
public:
	T1 id;
	son(T1 id,T2 name) {
		this->name = name;
		this->id = id;
	}
	void fun() {
		cout << this->name << endl;
		cout << this->id << endl;
	}
};

int main() {
	son<int,string> s(100,"Tom");
	s.fun();
	return 0;
}

 3.6类模板成员函数类外实现

代码实例:

#include <iostream>
using namespace std;
template<class T1,class T2>
class person {
public:
	T1 name;
	T2 id;
	person(T1 name, T2 id);
	void fun();
		
};

template<class T1, class T2>
person<T1,T2>::person(T1 name, T2 id) {
	this->name = name;
	this->id = id;
}

template<class T1, class T2>
void person<T1, T2>::fun() {
	cout << name << endl;
	cout << id << endl;
}

int main() {
	person<string, int> p("tom", 100);
	p.fun();
	return 0;
}

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

3.7类模板分文件编写

问题:

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

解决

·解决方式1:直接包含.cpp源文件
·解决方式2∶将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制

 代码:

person.hpp

#include <iostream>
using namespace std;

template<class T1 = string, class T2 = int>
class person {
public:
	T1 name;
	T2 age;
	person(T1 name, T2 age);
	void person_show();
};

template<class T1, class T2>
person<T1, T2>::person(T1 name, T2 age) {
	this->name = name;
	this->age = age;
}

template<class T1, class T2>
void person<T1, T2>::person_show() {
	cout << this->name << endl << this->age << endl;
}

person.cpp

#include "person.hpp";
int main() {
	person<> p("猪八戒", 100);
	p.person_show();
	return 0;
}

3.8类模板和友元

全局函数类内实现-直接在类内声明友元即可 
全局函数类外实现·需要提前让编译器知道全局函数的存在 

类内实现 

 代码:

#include <iostream>
using namespace std;
template<class T1=string,class T2=int>
class person {
//定义友元函数,属于全局函数
	friend void person_show(person<T1,T2> p) {
		cout << p.name << endl << p.age << endl;
	}
public:
	person(T1 name, T2 age);
private:
	T1 name;
	T2 age;
};

template<class T1, class T2>
person<T1,T2>::person(T1 name, T2 age) {
	this->name = name;
	this->age = age;
}


int main() {
	person<> p("小明", 100);
	person_show(p);
	return 0;
}

 

 类外实现

代码:

#include <iostream>
using namespace std;

//先声明有person类,防止报错
template<class T1, class T2>
class person;

//写在person定义之前,防止找不到报错
template<class T1, class T2>
void person_show(person<T1, T2> &p) {
	cout << p.name << endl << p.age << endl;
}

template<class T1,class T2>
class person {
	//friend void person_show(person<T1, T2>& p);  
	//错误写法,因为person_show是模板函数,此时T1是模板没的T1
	friend void person_show<>(person<T1, T2>& p);
	//正确写法,加入模板列表声明是模板
public:
	person(T1 name, T2 age);
private:
	T1 name;
	T2 age;
};

template<class T1, class T2>
person<T1,T2>::person(T1 name, T2 age) {
	this->name = name;
	this->age = age;
}


int main() {
	person<string, int> p("猪八戒", 100);
	person_show(p);
	return 0;
}

总结:建议全局函数做类内实现,用法简单,而且编译器可以直接识别 

类模板案例

案例描述: 实现一个通用的数组类,要求如下
。可以对内置数据类型以及自定义数据类型的数据进行存储
。将数组中的数据存储到堆区
。构造函数中可以传入数组的容量
。提供对应的拷贝构造函数以及operator=防止浅拷贝问题。

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

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

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

代码 

myArray.hpp

#pragma
#include <iostream>
using namespace std;

template<class T>
class myArray {
public:
	myArray(int capacity);
	myArray(myArray& p);
	~myArray();

	//重载=,防止浅拷贝
	myArray& operator=(const myArray& p) {
		if (this->Array != NULL) {
			delete[] this->Array;
			this->Array = NULL;
		}
		this->capacity = p.capacity;
		this->size = p.size;
		this->Array = p.Array;
		for (int i = 0; i < this->size; ++i) {
			this->Array[i] = p.Array[i];
		}
	}
	T& operator[](int index) {
		return this->Array[index];
	}
	void push_back(const T& val);
	void pop_back();
	int getsize() {
		return this->size;
	}
	int getcapacity() {
		return this->capacity;
	}
private:
	T* Array;
	int size;
	int capacity;
};

//拷贝构造函数,注意要用深拷贝
template<class T>
myArray<T>::myArray(myArray& p) {
	this->size = p.size;
	this->capacity = p.capacity;
	this->Array = new T[this->capacity];
	for (int i = 0; i < this->size; ++i) {
		this->Array[i] = p.Array[i];
	}
}

//有参构造函数
template<class T>
myArray<T>::myArray(int capacity) {
	this->capacity = capacity;
	this->Array = new T[this->capacity];
	this->size = 0;
}

//析构函数
template<class T>
myArray<T>::~myArray() {
	if (this->Array != NULL) {
		delete[] this->Array;
		this->Array = NULL;
	}	
}

template<class T>
void myArray<T> :: push_back(const T& val) {
	if (this->size == this->capacity) {
		return;
	}
	this->Array[this->size++] = val;
}

template<class T>
void myArray<T> ::pop_back() {
	if (this->size == 0) {
		return;
	}
	--this->size;
}

main.cpp

#include "myArray.hpp"
class person {
public:
	string name;
	int age;
	person() {}
	person(string name, int age) {
		this->name = name;
		this->age = age;
	}
};


void myprintf(myArray<person>& p) {
	for (int i = 0; i < p.getsize(); ++i) {
		cout << p[i].name << "  " << p[i].age << endl;
	}
}


void fun() {
	person p1("小明", 100);
	person p2("小壮", 130);
	person p3("小聪", 103);
	person p4("小红", 1560);
	person p5("小懒", 190);
	//调用默认构造函数
	myArray<person> p(10);
	p.push_back(p1);
	p.push_back(p2);
	p.push_back(p3);
	p.push_back(p4);
	p.push_back(p5);

	cout << p.getsize() << endl;
	cout << p.getcapacity() << endl;
	myprintf(p);
	p.pop_back();
	cout << "弹出最后一个" << endl;
	myprintf(p);
	cout << "判断拷贝构造函数" << endl;
	myArray<person> p6(p);
	myprintf(p6);
	cout << "判断operator=" << endl;
	myArray<person> p7(0);
	p7 = p;
	myprintf(p7);
}
int main() {
	fun();
	return 0;
}

更多推荐

LabVIEW使用ModbusTCP协议构建分布式测量系统

LabVIEW使用ModbusTCP协议构建分布式测量系统分布式测量系统主要用于监控远程物体。这种系统允许对系统用户获得的数据进行全面的数据收集、处理、存储和组织访问。它们可能包括许多不同类型的传感器。在任何具有互联网接入的个人计算机上运行的软件都会发送来自传感器的测量数据请求,接收来自控制器的响应,并将接收的值保存在

Docker 网络学习

docker的网络模式当你开始大规模使用Docker时,你会发现需要了解很多关于网络的知识。Docker作为目前最火的轻量级容器技术,有很多令人称道的功能,如Docker的镜像管理。然而,Docker同样有着很多不完善的地方,网络方面就是Docker比较薄弱的部分。因此,我们有必要深入了解Docker的网络知识,以满足

深度学习修炼(二)全连接神经网络 | Softmax,交叉熵损失函数 优化AdaGrad,RMSProp等 对抗过拟合 全攻略

文章目录1多层感知机(全连接神经网络)1.1表示1.2基本概念1.3必要组成—激活函数1.4网络结构设计2损失函数2.1SOFTMAX操作2.2交叉熵损失函数3优化3.1求导计算过于复杂?3.2链式法则导致的问题?3.3梯度下降算法的改进3.3.1动量法3.3.2自适应梯度方法1AdaGrad2RMSProp3.3.1

【Docker】Docker简介

Docker简介📋导航1.Docker简介1.1什么是Docker?1.2什么是容器?1.3容器的优势?1.4Docker的优势?1.5虚拟技术与容器技术Docker的区别?1.6为什么学习Docker?2.安装Docker3.Docker架构4.Docker命令4.1Docker镜像(1)查看所有的镜像(2)搜索镜

学习分布式第一天(分布式系统原理和概念)

目录分布式系统原理和概念1.分布式系统:单体架构:垂直架构:分布式架构:2.分布式计算:3.CAP原理:4.BASE理论:5.Paxos算法:6.Raft算法:分布式系统原理和概念1.分布式系统:学习分布式,肯定的知道什么是分布式。分布式言简意赅,就是多台电脑部署实现你要实现的功能,突破了单机的性能机器的瓶颈(高性能)

机器学习——特征工程和评价指标

0、前言:首先学习特征工程这部分知识之前,要对机器学习的算法用过使用。1、特征工程:就机器学习的数据而言,特征就是数据的列名,有多少列,就有多少个维度的特征。就定义而言,特征是指数据中抽取出来对结果预测有用的信息。特征工程就是使用一些技巧来处理数据,使数据特征能在机器学习算法中发挥更好的作用本质而言,特征工程其实就是数

【Tensorflow 2.12 电影推荐系统之排序模型】

Tensorflow2.12电影推荐系统之排序模型学习笔记导入相关模块准备数据加载数据数据预处理获取词汇表构建模型定义评分排序模型定义损失函数以及模型评估指标定义完整的评分排序模型训练和评估创建排序模型实例缓存数据训练评估预测导出和加载模型结尾学习笔记Tensorflow2.12智能电影推荐系统搭建学习笔记~Tenso

电压放大器在电子测试中的应用有哪些方面

电压放大器是一种常见的电子设备,广泛应用于各种测试和测量应用中。以下是电压放大器在电子测试中的几个主要方面应用的简要介绍。信号采集与处理:电压放大器通常用于信号采集和处理,在测试过程中将低电平信号放大到适合进一步处理或分析的水平。例如,在生物医学领域,电压放大器常用于心电图和脑电图等生理信号的采集和放大。它们能够将微弱

单片机内存管理

源码说明源码包含memory.h和memory.c两个文件(嵌入式C/C++代码的“标配”),其源码中包含重要的注释。memory.h文件包含结构体等定义,函数API申明等;memory.c文件是实现内存管理相关API函数的原型。memory.h头文件是相关的定义和申请:#ifndef__MEMORY_H__#defi

Python爬虫实战案例——第五例

文章中所有内容仅供学习交流使用,不用于其他任何目的!严禁将文中内容用于任何商业与非法用途,由此产生的一切后果与作者无关。若有侵权,请联系删除。目标:采集三国杀官网的精美壁纸地址:aHR0cHM6Ly93d3cuc2FuZ3Vvc2hhLmNvbS9tc2dzL21XYWxsUGFwZXI=从开发者工具中进行分析可以看到

不可变集合的详细概述

1.不可变集合1.1什么是不可变集合是一个长度不可变,内容也无法修改的集合1.2使用场景如果某个数据不能被修改,把它防御性地拷贝到不可变集合中是个很好的实践。当集合对象被不可信的库调用时,不可变形式是安全的。简单理解:不想让别人修改集合中的内容比如说:1,斗地主的54张牌,是不能添加,不能删除,不能修改的2,斗地主的打

热文推荐