模版进阶篇章

news/2024/9/24 21:19:58/

非类型模版参数

回顾:函数模版 :不用传类型,编译器会自动推导,和普通的函数调用一样

#include<iostream>
using namespace std;
template<typename T>// T是类型
bool Less(T a, T b)// a,b是T实例化的的对象
{return a < b;
}
int main()
{cout << Less<int>(1,2) << endl;cout << Less(1, 2) << endl;return 0;
}

类模版:通过传不同的类型,编译器会自动生成不同的类,然后进行实例化对象

namespace bit
{template<typename T>// T是类型class array{array( int size = 1 ){_array = new T[size];_size = size;}bool empty(){return _size == 0;}int size(){return _size;}private:T* _array;int _size;};
}

注意:调用函数模版时,需要传对象(匿名实例化 或者 有名实例化)         

           调用类模版时,需要传类型

#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
int main()
{
//函数模版vector<int> s = { 1, 2, 4, 6, 7,3,1 };sort(s.begin(), s.end(), less<int>());//升序for (auto& e : s)cout << e << " ";cout << endl;sort(s.begin(), s.end(), greater<int>());//降序for (auto& e : s)cout << e << " ";cout << endl;
//类模版priority_queue<int, vector<int>, less<int>> k;// 传的是类型k.push(1);k.push(3);k.push(9);k.push(10);while (!k.empty()){cout << k.top() << " ";k.pop();}cout << endl;return 0;
}

非类型模版参数

概念:将常量作为模版参数

注意:1.只有整形家族才能支持非类型模版参数

           2.参数在编译时就能确定

#include<iostream>
#include<algorithm>
using namespace std;
namespace bit
{template<typename T ,size_t size = 10>class array//静态数组{public:array( ){_array = new T[size];_size = size;}bool empty(){return _size == 0;}int size(){return _size;}private:T* _array;int _size;};
}
int main()
{bit::array<int, 20> s;return 0;
}

模版参数可以支持缺省参数(类似于函数)

在priority_queue中我们以vector作为priority_queue的底层(参考我的另一篇文章:详细的讲解priority_queuede的用法与底层https://blog.csdn.net/wx20041102/article/details/138355281?spm=1001.2014.3001.5501)

模版特化

通常情况下,使用模板可以实现一些与类型无关的代码,但对于一些特殊类型的可能会得到一些错

误的结果

列如:

#include<iostream>
#include<deque>
#include<algorithm>
#include<vector>template<typename T>
class Less
{
public:bool operator() (const T& x, const T& y){return x < y;}
};template<typename T>
class Greater
{
public:bool operator() (const T& x, const T& y){return y < x;}
};class Date
{
public:Date(int year, int month, int day): _year(year), _month(month), _day(day){}bool operator<(const Date& s){if (s._year > _year)return true;else if (s._year == _year){if (s._month > _month)return true;else if (s._month == _month){if (s._day > _day)return true;}}return false;}
private:int _year;int _month;int _day;
};
int main()
{Date* p1 = new Date(2024, 1, 1);Date* p2 = new Date(2024, 1, 4);Less<Date*> ls;cout << ls(p1 , p2) << endl;//这里比较的是地址大小,而我们想要比较指向的内容大小return 0;
}

解决方案:特化

  • 函数模版特化

步骤如下:

1.必须要有一个基础的函数模版

2.关键字template后接一对空的<>

3.直接在()中写出要特化的类型

4.函数形参表:必须要和模版参数的基础类型完全相同

#include<iostream>
#include<vector>
using namespace std;template<typename T>
bool Less(T& x, T& y)
{return x < y;
}template<typename T>
bool Less(int*& x, int*& y)
{return *x < *y;
}template<typename T>
bool Less(string*& x, string*& y)
{return *x < *y;
}
int main()
{int* p1 = new int(1);int* p2 = new int(3);cout << Less<int*>(p1, p2) << endl;return 0;
}

既然参数类型已经确定,那么函数模版也可以用函数重载(这是我们经常用的)

#include<iostream>
#include<vector>
using namespace std;template<typename T>
bool Less(T& x, T& y)
{return x < y;
}bool Less(int*& x, int*& y)
{return *x < *y;
}bool Less(string*& x, string*& y)
{return *x < *y;
}
int main()
{int* p1 = new int(1);int* p2 = new int(3);cout << Less<int*>(p1, p2) << endl;return 0;
}
  • 类模版特化

  • 全特化

类型全部确定

步骤如下:

1.template 后接一对<>

2.class 类型名 + < 类型 >//注意这里要将类模版中全部的参数写出来

#include<iostream>
#include<algorithm>
using namespace std;
template<typename T , typename K>
class date
{
public:date(){cout << "111111111" << endl;}
private:T _a;K _b;
};
template<>
class date<int,char>
{
public:date(){cout << "22222222222" << endl;}
private:int _a;char _b;
};int main()
{date<int, char> s;date<char, char> k;date<int, int> q;
}
  • 偏特化
部分特化

1.template< 需要实例化的类型 >//template<typename T>

2.class <需要实例化的类型 + 缺省类型 >// class < T , char >

#include<iostream>
#include<algorithm>
using namespace std;
template<typename T , typename K>
class date
{
public:date(){cout << "111111111" << endl;}
private:T _a;K _b;
};
template<>
class date<int,char>
{
public:date(){cout << "22222222222" << endl;}
private:int _a;char _b;
};
template<typename T >
class date< T , char >
{
public:date(){cout << "3333333333" << endl;}
private:T _a;char _b;
};
int main()
{date<int, char> s;date<char, char> k;date<int, int> q;
}
限制特化

以某个性质限制,列如指针 ,引用

#include<iostream>
#include<deque>
#include<algorithm>
#include<vector>template<typename T>
class Less
{
public:bool operator() (const T& x, const T& y){return x < y;}
};
template<typename T>
class Less<T*>// 限制特化 只有指针才能进入这里
{
public:bool operator()( T* x, T* y){return *x < *y;}
};template<typename T>
class Less<T&>// 限制特化 只有引用才能进入这里
{
public:bool operator()( T& x, T& y){return x < y;}
};template<typename T>
class Greater
{
public:bool operator() (const T& x, const T& y){return y < x;}
};class Date
{
public:Date(int year, int month, int day): _year(year), _month(month), _day(day){}bool operator<(const Date& s){if (s._year > _year)return true;else if (s._year == _year){if (s._month > _month)return true;else if (s._month == _month){if (s._day > _day)return true;}}return false;}
private:int _year;int _month;int _day;
};
int main()
{Date* p1 = new Date(2024, 1, 1);Date* p2 = new Date(2024, 1, 4);Less<Date*> ls;cout << ls(p1 , p2) << endl;return 0;
}


http://www.ppmy.cn/news/1456843.html

相关文章

spring框架学习记录(1)

前半个月一直在应付期中考试&#xff0c;快被折磨似了orz 文章目录 SpringIoC(Inversion of Control) 控制反转与DI(Dependency Injection)依赖注入bean相关bean配置bean实例化bean的生命周期 依赖注入相关依赖注入方式依赖自动装配 容器创建容器获取bean Spring IoC(Inversi…

电商平台遭遇DDOS、CC攻击有什么防护方案

电商平台遭遇DDOS、CC攻击有什么防护方案&#xff1f;在数字化浪潮的推动下&#xff0c;电商平台已成为现代商业的重要组成部分&#xff0c;为消费者提供便捷、多样的购物体验。然而&#xff0c;随着业务的发展&#xff0c;电商平台也面临着日益严峻的网络安全挑战&#xff0c;…

图片合称为视频

import cv2 import os def pic_video(args_input_path,folder_path,output_video_path): count 1 image_files [os.path.join(folder_path, file) for file in os.listdir(folder_path) if file.endswith(‘.png’)] img cv2.imread(image_files[0]) height img.shape[0] w…

单例、工厂、策略、装饰器设计模式

1. 单例模式&#xff08;Singleton Pattern&#xff09;&#xff1a; 单例模式是一种常用的设计模式&#xff0c;用于确保一个类只有一个实例&#xff0c;并提供一个全局访问点。这种模式的特点是类自己负责保存其唯一的实例&#xff0c;并控制其实例化过程。单例模式广泛应用…

GNU Radio FFT模块结合stream to vector应用及Rotator频偏模块使用

文章目录 前言一、FFT 模块应用1、stream to vector 介绍2、创建 grc 图测试3、运行结果 二、频偏模块1、Rotator 简介2、创建 grc 图测试3、运行结果 前言 写个博客记录一下自己的蠢劲儿&#xff0c;之前我想用 FFT 模块做一些信号分析的东西&#xff0c;官方的 FFT 模块必须…

Java使用JSch实现SSH远程执行命令

前言 有一个奇怪的需求&#xff0c;就是将本地的内容直接提交到远程服务器&#xff0c;并且需要针对文件夹进行处理。所以&#xff0c;这里就直接采用JSch来实现。在这里&#xff0c;感谢秀发浓密的程序猿的这篇博客&#xff0c;给了很大启发。 思路 既然是直接读取文件夹&am…

使用AIGC生成软件类图表

文章目录 如何使用 AI 生成软件类图表什么是 MermaidMermaid 的图片如何保存&#xff1f;mermaid.liveDraw.io Mermaid可以画什么图&#xff1f;流程图时序图 / 序列图类图状态图甘特图实体关系图 / ER图 如何使用 AI 生成软件类图表 ChatGPT 大语言模型不能直接生成各类图表。…

外网禅道配置

exportfs -avrf 修改代码&#xff0c;避免启动太慢&#xff1a;vi /opt/zbox/bin/zbox.php 启动和停止 /opt/zbox/zbox start /opt/zbox/zbox stop