函数模板
模板的意义: 对类型也可以进行参数化了
函数模板 是不进行编译的
模板实例化 函数调用的点进行实例化
模板函数 才是要被编译器编译的
模板类型参数
模板的实参推演
模板的特例化 特殊的实例化 不是编译器提供的 而是用户提供的
模板代码是不能在一个文件中定义 在另一个文件中使用的,模板代码在调用之前一定要看到模板定义的地方,这样模板才能够进行正常的实例化,产生能够被编译器编译的代码
所以模板代码都是放在头文件当中的,然后在源文件当中直接用#include包含
#include <iostream>
using namespace std;
template<typename T> //定义一个模板参数列表
bool compare(T a, T b) //compare是一个函数模板
{
cout << "template compare" << endl;
return a > b;
}
//针对const char*类型进行模板的特例化
template <>
bool compare<const char *>(const char * a, const char *b)
{
cout << "template compare(const char *) " << endl;
return strcmp(a, b)>0;
}
/*在函数调用点,编译器用用户指定的类型,从原模板实例化一份函数代码出来
*/
int main()
{
//函数调用点 根据用户指定的类型进行实例化
compare <int >(10, 20);
compare <double>(10.5, 20.5);
//函数模板实参推演
compare(20, 30);
compare("aaa", "bbb");//调用特例化的模板函数
return 0;
}
非类型参数
#include <iostream>
using namespace std;
template <typename T,int SIZE>//T是模板类型参数 SIZE是模板非类型参数
void sort(T *arr)
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE - 1; ++j)
{
if (arr[j] > arr[j+1])
{
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
int main()
{
int arr[] = { 12,23,34,45,3,2,43,54 };
const int size = sizeof(arr) / sizeof(arr[0]);
sort<int ,size>(arr);//模板函数的调用
for (int val : arr)
{
cout << val <<" ";
}
cout << endl;
return 0;
}
类模板
#include <iostream>
using namespace std;
/*
类模板
*/
template <typename T>
class Seqstack //模板名称+类型参数列表 构成类名
{
public:
Seqstack(int size = 10) :
_size(size),
_pstack(new T[size]),
_top (0)
{ }
~Seqstack()
{
delete[]_pstack;
_pstack = nullptr;
}
Seqstack(const Seqstack<T> &stack)
:_top(stack._top),
_size(stack._size)
{
_pstack = new T[_size];
for (int i = 0; i < _top; ++i)
{
_pstack[i] = stack._pstack[i];
}
}
Seqstack<T>& operator=(const Seqstack<T> &stack)
{
if (this == &stack) return *this;
delete[]_pstack;
_top = stack._top;
_size = stack._size;
_pstack = new T[_size];
for (int i = 0; i < _top; ++i)
{
_pstack[i] = stack._pstack[i];
}
return *this;
}
void push(const T &val)
{
if (full())
expand();
_pstack[_top++] = val;
}
void pop()
{
if (empty()) return;
--_top;
}
T top() const
{
if (empty())
throw "stack is empty!";//抛异常
return _pstack[_top-1];
}
bool full()const
{
return (_top == _size);
}
bool empty()const
{
return _top == 0;
}
private:
T *_pstack;
int _top;
int _size;
void expand()
{
T *ptmp = new T[_size * 2];
for (int i = 0; i < _top; ++i)
{
ptmp[i] = _pstack[i];
}
delete[]_pstack;
_pstack = ptmp;
_size *= 2;
}
};
int main()
{
Seqstack<int> s1;
s1.push(20);
s1.push(35);
s1.pop();
cout << s1.top() << endl;
return 0;
}