您的位置:首页 > 其它

函数模板的使用,对数字,字符的排序,

2014-10-15 13:27 288 查看
#include <iostream>

template <typename T>
void sort(T *a,T n){
T temp;

for(int i=0;i<n;i++){

for(int j=0;j<n-i-1;j++){

if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

for(int i=0;i<n;i++){
printf("%d\n",a[i]);
}
}

template <typename T1>
void sortchar(T1 *a,int n){

printf("sortchar\n");

char temp;

for(int i=0;i<n;i++){

for(int j=0;j<n-i-1;j++){

if(strcmp( &a[j],&a[j+1]) >0 ){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

for(int i=0;i<n;i++){
printf("%c",a[i]);
}
}

int main(int argc,
const char * argv[])
{

int a[10]={2,3,4,11,5,7,8,6,9,0};

sort(a,10);

printf("-------\n");

char b[]={'d','b','c'};

sortchar(b,3);

printf("_______");

char y = 'y';

char z = 'z';

if(strcmp(&y,&z)>0){

printf("%c",y);
}else{

printf("%c",z);
}

getchar();

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐