1.实验目的和要求:
(1) 理解函数模板、类模板,应用模板函数、模板类。
2. 实验内容
(1)用函数模板方式设计一个函数模板sort<T>,采用直接插入排序方式对数据进行排序,并对整数序列和字符序列进行排序。
template < class T>
void sort(T a[], int n)
#include<iostream>
using namespace std;
template < class T>
void sort(T a[], int n)
{
T temp;
int index;
for(int i=1; i<n; i++)
{
temp=a[i];
index=i-1;
while(index>=0&&temp<a[index])//寻找插入点
{
a[index+1]=a[index];
index--;
}
a[index+1]=temp;//插入
}
}
template<class T>
void output(T a[],int n)
{
for(int j=0; j<8; j++)
cout<<a[j]<<" ";
cout<<endl;
}
int main()
{
int a[8]= {10,34,23,56,79,39,19,20};
char c[9]&#