#include<iostream>
#include<string>
using namespace std;
template <typename T>
T sort(T x[],int m){ //排序函数
int i,j,k;
T t;
for(i=0;i< m-1;i++)
{ k=i;
for(j=i+1;j< m;j++)
if(x[j]< x[k])
k=j;
if(k!=i)
{ t=x[i];x[i]=x[k];x[k]=t;}}return x[j];}
char sort(char x[],int m){ //字符排序重载
int i,j,k;
char t;
for(i=0;i< m-1;i++)
{ k=i;
for(j=i+1;j< m;j++)
if(x[j]< x[k])
k=j;
if(k!=i)
{ t=x[i];x[i]=x[k];x[k]=t;}}return x[j];}
//字符串数组的排序参考如下:
void sort(char str[20][20], int n) {
char a[20];
int i, j;
for (i = 0; i < n-1; i++) {
for (j = i ; j < n-1; j++)
if (strcmp(str[j], str[j + 1]) > 0) {
strcpy(a, str[j]);
strcpy(str[j], str[j + 1]);
strcpy(str[j+1], a);}return str[j];}
int main()
{
int a[]={19,25,56,45,15};
const int m=sizeof(a) / sizeof(*a);
sort(a,m);
for(int i=0;i<m;i++)
{cout<<a[i]<<"\t";}
cout<<"\n";
double b[]={1.0,5.2,4.3,6.2,2.5};
const int n=sizeof(b) / sizeof(*b);
sort(b,n);
for(int j=0;j<n;j++)
{cout<<b[j]<<"\t";}
cout<<"\n";
char c[]={'a','b','y','u','r'};
const int p=sizeof(c) / sizeof(*c);
sort(c,p);
for(int k=0;k<p;k++)
{cout<<c[k]<<"\t";}
cout<<"\n";
char s[][]={"hsdfkjf","dhfjsdfs"}
const int q=sizeof(s) / sizeof(*s);
sort(s[][],q);
for(int d=0;d<q;d++)
{cout<<s[j][j+1]<<"\t";}
cout<<"\n";
return 0;
}
额 那个主函数中的字符数组是我写的,有问题,好像!
烙印99
慕标琳琳
相关分类