template R Sum(const T *array, int n)
{
R sum = 0
for (int i = 0 i <n ++i)
sum += i
return sum
}
如果不是内置类型,该模板隐式地需要 有R R::operator+=(T)运算符可用。
1. 使用函数指针作为 Functor 替换者
Typedef struct tagAddClass
{
Void (*add)(char* r1, const char* r2)
Int elemSize
Char sum[MAX_ELEM_SIZE]
} AddClass
void Sum(AddClass* self, const char* array, int n)
{
for (int i = 0 i <n ++i)
self->add(self->sum, array + i*self->elemSize)
}
使用时:
Void AddInt(char* r1, const char* r2) {
*(long*)r1 += *(int*)r2}
AddClass addClass = {AddInt, 2, 0 }
Int array[100]
Read(array)
Sum(&addClass, array, 100)…..
2. 用宏作为Functor的替换者
#define GenSumFun(SumFunName, Add, RetType, ElemType)
RetType SumFunName (const ElemType *array, int n) \
{
RetType sum = 0
for (int i = 0 i <n ++i)
Add(sum, i)
return sum}
使用时:
#define AddInt(x, y) ((x) += (y))
GenSumFun(SumInt, AddInt, long, int) …..
Int array[100]
Read(array)
Long sum = SumInt(array, 100)…..
3. 所有可替换参数均为宏
至少需要一个额外的文件(实现文件)为 impsum.c
/* impsum.c */
RetType FunName(const ElemType *array, int n)
{
RetType sum = 0
for (int i = 0 i <n ++i)
Add(sum, i)
return sum}
使用时:
#undef RetType
#undef FunName
#undef ElemType
#undef Add
#define AddInt(x, y) ((x) += (y))
#define RetType long
#define FunName SumInt
#define ElemType int
#define Add AddInt
#include impsum.c …..
Int array[100]
Read(array)
Long sum = SumInt(array, 100)
4. 总结:
第一种方法,易于跟踪调试,但是效率低下,适用于对可变函数(函数指针)的效率要求不高,但程序出错的可能性较大(复杂),模板函数(Sum)本身很复杂,模板参数也比较复杂(add)的场合。
第二种方法,效率高,但很难跟踪调试,在模板函数和模板参数本身都很复杂的时候更是如此。
第三种方法,是我最近几天才想出的,我认为是最好的,在模板参数(Add)比较复杂时可以用函数(第二种也可以如此),简单时可以用宏,并且,易于调试。在模板函数本身很复杂,而模板参数比较简单时更为优越。但是,可能有点繁琐。
首先,C没有函数模版。C++才有。其次,template <class T>是函数声明的一部分,所以下面函数实现应该是:
template <class T>
void swap(T &a,T &b){
int temp
temp=a
a=b
b=temp
}
最后,#include <iostream>,在标准的C++函数中,std的域中已经有一个swap函数。
而且前面也using namespace了。函数声明重复。
两个办法:
1 swap(i,j)改为 ::swap(i,j)//全局化。
2 swap改个名字。