//非类型模板参数 -- 常量
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. 非类型的模板参数必须在编译期就能确认结果
int main()
{
array<int, 10> a1;
int a2[10] = { 0 };
//a1[10]; 越界检查--operator()函数调用
//a2[15] = 0; 写会检查(部分会检查)读不检查
return 0;
}
在原模板类的基础上,针对特殊类型进行特殊化的实现方式。分为函数模板特化与类模板特化。
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
#include
#include
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;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
#include
#include
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 Compare = less >
//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;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
//类基础模板
template<class T1, class T2>
class Data
{
public:
Data()
{
cout << "Data" << endl;
}
private:
T1 _d1;
T2 _d2;
};
// 全特化:将模板参数列表中所有参数确定化
//template
template<>
class Data<int, char>
{
public:
Data()
{
cout << "Data" << endl;
}
private:
int _d1;
char _d2;
};
//偏特化
//template
template <class T1>
class Data<T1, int>
{
public:
Data()
{
cout << "Data" << endl;
}
private:
T1 _d1;
int _d2;
};
//template
template<class T1, class T2>
class Data<T1*,T2*>
{
public:
Data()
{
cout << "Data" << endl;
}
private:
T1 _d1;
T2 _d2;
};
//template
template <class T1, class T2>
class Data <T1&, T2&>
{
public:
Data()
{
cout << "Data" << endl;
}
};
//template
template<class T1, class T2>
class Data<T1&, T2*>
{
public:
Data()
{
cout << "Data" << 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;
}
当出现这种情况: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;
}
#pragma once
#include
#include
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;
};
}
#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;
}
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
#include
#include
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;
}
优点: