模板参数可以是数值型参数(非类型参数)
template
void func()
{
T a[N]; //使用模板参数定义局部数组
}
func();
数值型模板参数的限制:变量不能作为模板参数,浮点数(计算机内部表示不精确)不能作为模板参数,类对象不能作为模板参数。
本质:模板参数是在编译阶段被处理的单元,因此,在编译阶段必须准确无误的唯一缺点。
最高效的方法求1+2+3+..+N的和?
#include
#include
using namespace std;
template
< typename T, int N >
void func()
{
T a[N] = {0};
for(int i=0; i {
a[i] = i;
}
for(int i=0; i {
cout << a[i] << endl;
}
}
template //x
< int N >
class Sum
{
public:
static const int VALUE = Sum
::VALUE + N; //存储在全局数据区或者符号表,value存储在全局数据区
};
template
< > //完全特化
class Sum < 1 >
{
public:
static const int VALUE = 1; //符号表
};
//static 静态成员,具有类变量属性,使得整个类可以使用,存储在全局存储区,const表示进入符号表,运行之前就能确定他的值
int main()
{
//int a=10;
// func(); //出错
cout << "1 + 2 + 3 + ... + 10 = " << Sum<10>::VALUE << endl;
cout << "1 + 2 + 3 + ... + 100 = " << Sum<100>::VALUE << endl;
//N=10,调用x,在调用sum<9>...sum<1>,值为1,编译器做相应计算,在编译期就能确定结果值,运行时直接打印值。
return 0;
}
#ifndef _ARRAY_H_
#define _ARRAY_H_
template
< typename T, int N >
class Array
{
T m_array[N];
public:
int length();
bool set(int index, T value);
bool get(int index, T& value);
T& operator[] (int index);
T operator[] (int index) const;
virtual ~Array();
};
template
< typename T, int N >
int Array::length()
{
return N;
}
template
< typename T, int N >
bool Array::set(int index, T value)
{
bool ret = (0 <= index) && (index < N);
if( ret )
{
m_array[index] = value;
}
return ret;
}
template
< typename T, int N >
bool Array::get(int index, T& value)
{
bool ret = (0 <= index) && (index < N);
if( ret )
{
value = m_array[index];
}
return ret;
}
template
< typename T, int N >
T& Array::operator[] (int index)
{
return m_array[index];
}
template
< typename T, int N >
T Array::operator[] (int index) const //数组类对象是const对象,最后加const
{
return m_array[index];
}
template
< typename T, int N >
Array::~Array()
{
}
#endif
第34课intarray 扩展:
#ifndef _HEAPARRAY_H_ //heap 堆上分配空间
#define _HEAPARRAY_H_
template
< typename T >
class HeapArray
{
private:
int m_length;
T* m_pointer;
HeapArray(int len); //构造函数时private, 不能继承
HeapArray(const HeapArray& obj);
bool construct();
public:
static HeapArray* NewInstance(int length);
int length();
bool get(int index, T& value);
bool set(int index ,T value);
T& operator [] (int index);
T operator [] (int index) const;
HeapArray& self();
~HeapArray();
};
template
< typename T >
HeapArray::HeapArray(int len)
{
m_length = len;
}
template
< typename T >
bool HeapArray::construct()
{
m_pointer = new T[m_length];
return m_pointer != NULL;
}
template
< typename T >
HeapArray* HeapArray::NewInstance(int length)
{
HeapArray* ret = new HeapArray(length);
if( !(ret && ret->construct()) )
{
delete ret;
ret = 0;
}
return ret;
}
template
< typename T >
int HeapArray::length()
{
return m_length;
}
template
< typename T >
bool HeapArray::get(int index, T& value)
{
bool ret = (0 <= index) && (index < length());
if( ret )
{
value = m_pointer[index];
}
return ret;
}
template
< typename T >
bool HeapArray::set(int index, T value)
{
bool ret = (0 <= index) && (index < length());
if( ret )
{
m_pointer[index] = value;
}
return ret;
}
template
< typename T >
T& HeapArray::operator [] (int index)
{
return m_pointer[index];
}
template
< typename T >
T HeapArray::operator [] (int index) const
{
return m_pointer[index];
}
template
< typename T >
HeapArray& HeapArray::self()
{
return *this;
}
template
< typename T >
HeapArray::~HeapArray()
{
delete[]m_pointer;
}
#endif
#include
#include
#include "Array.h"
#include "HeapArray.h"
using namespace std;
int main()
{
Array ad;
for(int i=0; i {
ad[i] = i * i;
}
for(int i=0; i {
cout << ad[i] << endl;
}
cout << endl;
//以下是heaparray 的测试
HeapArray* pai = HeapArray::NewInstance(10);
if( pai != NULL )
{
HeapArray& ai = pai->self();
for(int i=0; i {
ai[i] = i + 'a';
}
for(int i=0; i {
cout << ai[i] << endl;
}
}
delete pai;
return 0;
}
模板参数可以是数值型参数,数值型模板参数必须在编译期间唯一确定,数组类模板是基于数值型模板参数实现的,数组类模板是简易的线性表数据结构。