编写一个函数模板,至少使用五种方法反转元素,例如反转前n个元素、反转给定部分的元素、反转两个独立部分的元素或以自己的方式反转元素(函数对象)。然后,给出两个非模板函数(函数重载)。最后,在程序中测试它们。(确保您的程序对用户友好且清晰)
例如:
template < class T> void reverse(T a[], int n);
void reverse(double *a, int n);
template <class T>
void reverse(T * a, int startindex, int n)
{
T t;
for (int i = startindex; i < startindex + n / 2; i++)
{
t = a[i];
a[i] = a[startindex+n-i+startindex-1];
a[startindex+n-i+startindex-1] = t;
}
}
void reverse(double * a, int n)
{
reverse(a, 0, n);
}