2 回答

TA贡献1829条经验 获得超13个赞
你好!
问题挺多的,我给你改了!
#include<iostream>
#include<string>
#include "string.h"
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[][20]={"hsdfkjf","dhfjsdfs"};
const int q=sizeof(s) / sizeof(*s);
sort(s,q);
for(int d=0;d<q;d++)
{cout<<s[d]<<"\t";}
cout<<"\n";
return 0;
}

TA贡献1830条经验 获得超9个赞
char s[][20] = { "hsdfkjf" , "dhfjsdfs" }; //第2维 数组 固定长度 const int q = sizeof (s) / sizeof (*s); sort(s[0], strlen (s[0]));——>直接用模板 sort(s[1], strlen (s[1])); for ( int d = 0; d<q; d++) { cout << s[d] << "\t" ; } cout << "\n" ; return 0; |
添加回答
举报