为了账号安全,请及时绑定邮箱和手机立即绑定

如下程序,请问一下,怎样用模板对字符串惊醒排序?

如下程序,请问一下,怎样用模板对字符串惊醒排序?

30秒到达战场 2022-05-19 19:15:07
#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;}额 那个主函数中的字符数组是我写的,有问题,好像!
查看完整描述

2 回答

?
烙印99

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;
}



查看完整回答
反对 回复 2022-05-23
?
慕标琳琳

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;


查看完整回答
反对 回复 2022-05-23
  • 2 回答
  • 0 关注
  • 108 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号