• 模板(1)


    1、非类型模板参数—— 常量(适用于整型数据)

    #include 
    #include 
    #include 
    using namespace std;
    
    //1、非类型模板参数—— 常量(适用于整型数据)
    template<class T, size_t N = 10>
    class array
    {
    private:
    	T _a[N];
    };
    
    int main()
    {
    	array<int> a0;
    	array<int, 100> a1;      // 100
    	array<double, 1000> a2;  // 1000
    
    	return 0;
    }
    
    int main()
    {
    	array<int, 10> a1; // C++11
    	int a2[10];       // C
    
    	cout << sizeof(a1) << endl;
    	cout << sizeof(a2) << endl;
    
    	// 越界都能查出来 —— 函数调用
    	// a1[10];
    	// a1[15]=0;
    
    	// 指针解引用 —— 抽查是否越界,只针对越界写,越界读不检查
    	// a2[10];
    	// a2[10]=1;
    	// a2[15]=1;
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    2、模板的特化处理(特殊化处理)

    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;
    		}
    	}
    
    	int _year;
    	int _month;
    	int _day;
    };
    
    //(1)函数模板的特化
    template<class T>
    bool Greater(T left, T right)
    {
    	return left > right;
    }
    
    //特化 -- 针对某些类型进行特殊化处理
    template<>
    bool Greater<Date*>(Date* left, Date* right)
    {
    	return *left > *right;
    }
    
    /
    //(2)类模板的特化
    
    namespace jpc
    {
    	template<class T>
    	struct greater
    	{
    		bool operator()(const T& x1, const T& x2)const
    		{
    			return x1 > x2;
    		}
    	};
    
    	//特化:【即使是输入日期的地址,也能比较出各个日期的大小】
    	template<>
    	struct greater<Date*>
    	{
    		bool operator()(Date* x1, Date* x2)const
    		{
    			return *x1 > *x2;
    		}
    	};
    
    }
    
    
    int main()
    {
    	//函数模板
    	cout << Greater(1, 2) << endl;  //可以比较,结果正确
    
    	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; //可以比较,结果错误
    
    	//类模板
    	jpc::greater<Date> lessFunc1;
    	cout << lessFunc1(d1, d2) << endl;
    
    	jpc::greater<Date*> lessFunc2;
    	cout << lessFunc2(p1, p2) << endl;
    
    
    	//优先级队列
    	std::priority_queue<Date, vector<Date>, jpc::greater<Date>> dq1;
    	std::priority_queue<Date*, vector<Date*>, jpc::greater<Date*>> dq2;
    
    	dq1.push(Date(2022, 9, 27));
    	dq1.push(Date(2022, 9, 20));
    	dq1.push(Date(2022, 9, 28));
    	dq1.push(Date(2022, 9, 26));
    	dq1.push(Date(2022, 9, 29));
    
    	while (!dq1.empty())
    	{
    		const Date& top = dq1.top();
    		cout << top._year << "/" << top._month << "/" << top._day << endl;
    		dq1.pop();
    	}
    	cout << endl;
    
    
    	dq2.push(new Date(2022, 9, 27));
    	dq2.push(new Date(2022, 9, 20));
    	dq2.push(new Date(2022, 9, 28));
    	dq2.push(new Date(2022, 9, 26));
    	dq2.push(new Date(2022, 9, 29));
    
    	while (!dq2.empty())
    	{
    		Date* top = dq2.top();
    		cout << top->_year << "/" << top->_month << "/" << top->_day << endl;
    		dq2.pop();
    	}
    	cout << endl;
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
  • 相关阅读:
    简单宠物网页设计作业 静态HTML动物介绍网页作业 DW宠物网站模板下载 大学生简单野生动物网页作品代码
    vue+antd——实现table表格的打印——分页换行,每页都有表头——基础积累
    Idea下面git的使用:变基、合并、优选、还原提交、重置、回滚、补丁
    基于kafka的日志收集分析平台
    PMP刷题小结
    面向对象【递归方法】
    树结构基础
    C语言——计算1!+2!+3!+......+10!
    ASP.NET Core 6框架揭秘实例演示[24]:中间件的多种定义方式
    《战地2042》DX报错无法启动游戏怎么处理,战地2042启动游戏DX报错解决方法
  • 原文地址:https://blog.csdn.net/pengpeng_code/article/details/133345871