当前位置: 首页 > news >正文

模板Plus

文章目录

  • 1.非类型模板参数的引入
  • 2.标准库和普通数组
  • 3.模板的特化

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

2.标准库和普通数组

int main()
{array<int, 10> a1;  int a2[10] = { 0 };//a1[10];      越界检查--operator()函数调用//a2[15] = 0;  写会检查(部分会检查)读不检查return 0;
}

3.模板的特化

#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;
}
http://www.lryc.cn/news/116187.html

相关文章:

  • spring事务和数据库事务是怎么实现
  • el-date-picker设置默认当前日期
  • vue中使用this.$refs获取不到子组件的方法,属性方法都为undefined的解决方法
  • Linux命令200例:df用于显示文件系统的磁盘空间使用情况
  • Service not registered 异常导致手机重启分析
  • 深度解读|一站式ABI平台 Smartbi Insight V11 能力再升级
  • vConsole手机调试模式uniapp和原生h5
  • Flutter Dart语言(05)异步
  • 滇医通微信小程序分析笔记
  • IoTDB在springboot2中的(二) 查询
  • SpringBoot 底层机制分析【Tomcat 启动+Spring 容器初始化+Tomcat 如何关联Spring 容器】【下】
  • NLP(六十五)LangChain中的重连(retry)机制
  • C字符串与C++ string 类:用法万字详解(上)
  • async/await函数需要trycatch吗?
  • Jenkins集成appium自动化测试(Windows篇)
  • MongoDB:切换log日志文件
  • 代码随想录第三十五天
  • 块、行内块水平垂直居中
  • Mybatis引出的一系列问题-动态 SQL
  • Docker学习之构建Base Image
  • SFM(Structure from Motion)和NeRF(Neural Radiance Fields)
  • [Vue] Vue2和Vue3的生命周期函数
  • springboot集成分布式任务调度系统xxl-job(调度器和执行器)
  • 11_Vue3中的新的组件
  • 详解推送Git分支时发生的 cannot lock ref 错误
  • [国产MCU]-BL602开发实例-PWM
  • 【JMeter】 使用Synchronizing Timer设置请求集合点,实现绝对并发
  • 无法对watchdog.sys等系统文件删除,弯道修复,这里解决办法很简单
  • ClickHouse(九):Clickhouse表引擎 - Log系列表引擎
  • 3.1 计算机网络和网络设备