模板Plus

1.非类型模板参数的引入

//非类型模板参数 -- 常量
template<class T, size_t N = 10>
class array
{
private:
	T _a[N];
};


int main()
{
	array<int> a1;
	array<int, 100> a2;
	array<double, 1000> a3;
	return 0;
}
1. 浮点数、类对象、字符串不允许作为非类型模板参数
2. 非类型的模板参数必须在编译期就能确认结果

2.标准库和普通数组

int main()
{
	array<int, 10> a1;  
	int a2[10] = { 0 };

	//a1[10];      越界检查--operator()函数调用
	//a2[15] = 0;  写会检查(部分会检查)读不检查

	return 0;
}

3.模板的特化

3.1介绍

在原模板类的基础上,针对特殊类型进行特殊化的实现方式。分为函数模板特化与类模板特化。

3.2代码讲解

#define _CRT_SECURE_NO_WARNINGS 
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
#include <array>
#include <time.h>
#include <queue>
#include <stdbool.h>
using namespace std;

struct Date
{
	//构造函数
	Date(int year, int month, int day)
		:_year(year)
		, _month(month)
		, _day(day)
	{
	
	}

	//判断大于
	bool operator>(const Date& d) const
	{
		if ((_year > d._year)
			|| (_year == d._year && _month > d._month)
			|| (_year == d._year && _month == d._month && _day > d._day))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	//判断小于
	bool operator<(const Date& d) const
	{
		if ((_year < d._year)
			|| (_year == d._year && _month < d._month)
			|| (_year == d._year && _month == d._month && _day < d._day))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	//成员属性
	int _year;
	int _month;
	int _day;
};

//函数模板及特殊化处理
template<class T>
bool Greater(T left, T right)
{
	return left > right;
}
template<>
bool Greater<Date*>(Date* left, Date* right)
{
	return *left > *right;
}

//类模板及特殊化处理
namespace apex
{
	template<class T>
	struct less
	{
		bool operator()(const T& left, const T& right) const
		{
			return left < right;
		}
	};

	template<>
	struct less<Date*>
	{
		bool operator()(Date* d1, Date* d2) const
		{
			return *d1 < *d2;
		}
	};
}

int main()
{
	//函数模板特殊化处理
	Date d1(2022, 7, 7);
	Date d2(2022, 7, 8);
	cout << Greater(d1, d2) << endl;  
	Date* p1 = &d1;
	Date* p2 = &d2;
	cout << Greater(p1, p2) << endl; 

	//类模板特殊化处理
	apex::less<Date> ls1;
	cout << ls1(d1, d2) << endl;
	apex::less<Date*> ls2;
	cout << ls2(p1, p2) << endl;

	//适配器模板的特化
	std::priority_queue<Date, vector<Date>, apex::less<Date>> dq1;
	std::priority_queue<Date*, vector<Date*>, apex::less<Date*>> dq2;
	dq2.push(new Date(2023, 8, 9));
	dq2.push(new Date(2023, 8, 10));
	dq2.push(new Date(2023, 8, 12));
	dq2.push(new Date(2023, 8, 13));

	return 0;
}

3.3画图讲解

在这里插入图片描述

4.类、函数模板特化初识

#define _CRT_SECURE_NO_WARNINGS 
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
#include <array>
#include <time.h>
#include <queue>
#include <stdbool.h>
using namespace std;

struct Date
{
	//构造函数
	Date(int year, int month, int day)
		:_year(year)
		, _month(month)
		, _day(day)
	{
	
	}

	//判断大于
	bool operator>(const Date& d) const
	{
		if ((_year > d._year)
			|| (_year == d._year && _month > d._month)
			|| (_year == d._year && _month == d._month && _day > d._day))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	//判断小于
	bool operator<(const Date& d) const
	{
		if ((_year < d._year)
			|| (_year == d._year && _month < d._month)
			|| (_year == d._year && _month == d._month && _day < d._day))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	//成员属性
	int _year;
	int _month;
	int _day;
};

//函数模板及特殊化处理
template<class T>
bool Greater(T left, T right)
{
	return left > right;
}
template<>
bool Greater<Date*>(Date* left, Date* right)
{
	return *left > *right;
}

//类模板及特殊化处理
namespace apex
{
	template<class T>
	struct less
	{
		bool operator()(const T& left, const T& right) const
		{
			return left < right;
		}
	};

	template<>
	struct less<Date*>
	{
		bool operator()(Date* d1, Date* d2) const
		{
			return *d1 < *d2;
		}
	};
}

int main()
{
	//函数模板特殊化处理
	Date d1(2022, 7, 7);
	Date d2(2022, 7, 8);
	cout << Greater(d1, d2) << endl;  
	Date* p1 = &d1;
	Date* p2 = &d2;
	cout << Greater(p1, p2) << endl; 

	//类模板特殊化处理
	apex::less<Date> ls1;
	cout << ls1(d1, d2) << endl;
	apex::less<Date*> ls2;
	cout << ls2(p1, p2) << endl;

	//适配器模板的特化
	//template <class T, class Container = vector<T>,class Compare = less<typename Container::value_type> > 
	//class priority_queue;

Date类型
	std::priority_queue<Date, vector<Date>, apex::less<Date>> dq1;
	dq1.push(Date(2023, 8, 10));
	dq1.push(Date(2023, 8, 11));
	dq1.push(Date(2023, 8, 12));
	dq1.push(Date(2023, 8, 13));
	while (!dq1.empty())
	{
		const Date& top = dq1.top();
		cout << top._year << "-" << top._month << "-" << top._day << endl;
		dq1.pop();
	}
	cout << endl;
Date*类型
	std::priority_queue<Date*, vector<Date*>, apex::less<Date*>> dq2;
	dq2.push(new Date(2023, 8, 10));
	dq2.push(new Date(2023, 8, 11));
	dq2.push(new Date(2023, 8, 12));
	dq2.push(new Date(2023, 8, 13));
	while (!dq2.empty())
	{
		Date* top = dq2.top();
		cout << top->_year << "-" << top->_month << "-" << top->_day << endl;
		dq2.pop();
	}

	return 0;
}

5.全特化与偏特化

#define _CRT_SECURE_NO_WARNINGS 
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
#include <array>
#include <time.h>
#include <queue>
#include <stdbool.h>
using namespace std;

//类基础模板
template<class T1, class T2>
class Data
{
public:
	Data()
	{ 
		cout << "Data<T1, T2>" << endl; 
	}
private:
	T1 _d1;
	T2 _d2;
};

// 全特化:将模板参数列表中所有参数确定化

//template<class T1, class T2>
template<>
class Data<int, char>
{
public:
	Data() 
	{ 
		cout << "Data<int, char>" << endl;
	}
private:
	int _d1;
	char _d2;
};

//偏特化
//template<class T1, class T2>
template <class T1>
class Data<T1, int>
{
public:
	Data() 
	{ 
		cout << "Data<T1, int>" << endl;
	}
private:
	T1 _d1;
	int _d2;
};

//template<class T1, class T2>
template<class T1, class T2>
class Data<T1*,T2*>
{
public:
	Data() 
	{ 
		cout << "Data<T1*, T2*>" << endl; 
	}
private:
	T1 _d1;
	T2 _d2;
};

//template<class T1, class T2>
template <class T1, class T2>
class Data <T1&, T2&>
{
public:
	Data()
	{
		cout << "Data<T1&, T2&>" << endl;
	}
};

//template<class T1, class T2>
template<class T1, class T2>
class Data<T1&, T2*>
{
public:
	Data()
	{
		cout << "Data<T1&, T2*>" << endl;
	}
};

int main()
{
	Data<int, int> d0;
	Data<double, int> d1;
	Data<int, char> d2;
	Data<double, double> d3;
	Data<double*, double*> d4;
	Data<int*, char*> d5;
	Data<int*, char> d6;
	Data<int&, char&> d7;
	Data<int&, double&> d8;
	Data<int&, double*> d9;

	return 0;
}

在这里插入图片描述

6.模板不能分离编译

1.typename的使用

当出现这种情况:vector < T >::iterator 在这里插入图片描述
即一个在另一个类里typedef的内嵌类型或内部类从属于一个模板时 需要在前面加typename 目的是告诉编译器 这个整体是一个类型而非变量 否则编译器无法识别而报错

template<class T>
void func(const vector<T>& v)
{
	typename vector<T>::iterator it = v.begin();
}
int main()
{
	vector<int> v;
	func(v);
	return 0;
}

2.预处理相关知识

在这里插入图片描述

3.为什么不能分离编译?

在这里插入图片描述

4.怎么解决?

  1. 声明和定义统一放到.h文件 即便要分离 也可以换一种方式分离:类内声明 类外定义
  2. 显示实例化:还按在两个文件分离,只不过需要加特定语句在这里插入图片描述
    需要什么类型就要加对应语句

5.代码详解

1.vector.h

#pragma once
#include <assert.h>
#include<iostream>
using namespace std;
namespace apex
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		vector()
			:_start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{

		}
		~vector()
		{
			delete[] _start;
			_start = _finish = _end_of_storage = nullptr;
		}
		size_t capacity() const
		{
			return _end_of_storage - _start;
		}
		size_t size() const
		{
			return _finish - _start;
		}
		const T& operator[](size_t pos) const
		{
			assert(pos < size());

			return _start[pos];
		}
		void push_back(const T& x)
		{
			insert(_finish, x);
		}
		void reserve(size_t n);
		iterator insert(iterator pos, const T& x);
	private:
		iterator _start;
		iterator _finish;
		iterator _end_of_storage;
	};
}

2.vector.cpp

#include"vector.h"
namespace apex
{
	template<class T>
	void vector<T>::reserve(size_t n)
	{
		if (n > capacity())
		{
			size_t sz = size();
			T* tmp = new T[n];

			if (_start != nullptr)
			{
				for (size_t i = 0; i < sz; ++i)
				{
					tmp[i] = _start[i];
				}
				delete[] _start;
			}

			_start = tmp;
			_finish = _start + sz;
			_end_of_storage = _start + n;
		}
	}
	template<class T>
	typename vector<T>::iterator vector<T>::insert(typename vector<T>::iterator pos, const T& x)
	{
		assert(pos >= _start && pos <= _finish);

		if (_finish == _end_of_storage)
		{
			size_t len = pos - _start;

			reserve(capacity() == 0 ? 4 : capacity() * 2); 
			pos = _start + len;                            
		}

		iterator end = _finish - 1;
		while (end >= pos)
		{
			*(end + 1) = *end;
			--end;
		}
		*pos = x;

		++_finish;

		return pos;
	}

}

3.test.cpp

#define _CRT_SECURE_NO_WARNINGS 
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
#include <array>
#include <time.h>
#include <queue>
#include <stdbool.h>
using namespace std;

#include"vector.h"

int main()
{
	apex::vector<int> v;
	v.reserve(10);
	for (size_t i = 0; i < v.size(); ++i)
	{
		cout << v[i] << " ";
	}
	cout << endl;
    return 0;
}

7.模板优缺点

优点:

  1. 增强复用性–减少代码量–介绍资源–使得开发迭代更快–是STL泛型编程的基础
  2. 增强灵活性
    缺点:
  3. 若实例化过多–代码膨胀问题–编译时间变长
  4. 编译器给出的编译错误信息杂乱–不易修正
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿猿收手吧!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值