#include<iostream>
using namespace std;
template<class elemtype>
void print(elemtype list[],int length);
//1.elemtype做为一个名称为什么可以定义list[](为什么不是int或class之类的)
//2.为什么此处要明确数值 我记得如果是数组的话可以直接(int[],int)
template<class elemtype>
void bubblesort(elemtype list[],int length);
int main()
{
int intlist[]={2,56,34,25,73,46,89,10,5,16};
cout<<"before sorting,intlist:";
print(intlist,10);
cout<<endl;
bubblesort(intlist,10);
cout<<"after sorting,intlist:";
print(intlist,10);
return 0;
}
template<class elemtype>
void print(elemtype list[],int length)
{
for(int i=0;i<length;i++)
cout<<list[i]<<" ";
cout<<endl;
}
template<class elemtype>
void bubblesort(elemtype list[],int length)
{
for(int a=1;a<length;a++)
{
for(int b=0;b<length-a;b++)
{
if(list[b]>list[b+1])
{
elemtype temp=list[b];
list[b]=list[b+1];
list[b+1]=temp;
}
}
}
}
回一楼 elemtype是名称 比如可以变成asdf什么的 或者你的意思是代表了class?
参考资料:http://babylindacjwpisces.spaces.live.com/blog/cns!79f2c469a939181b!232.entry