C++对字符数组排序

如下程序,请问一下,怎样用模板对字符串惊醒排序?
#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;
}
额 那个主函数中的字符数组是我写的,有问题,好像!

与其它排序类似,字符数组排序也是根据一定算法,如冒泡法,将各个项值进行比较,并通过赋值交换位置即可。

对于字符数组,赋值和比较均与一般对象或变量不同。

1 å­—符数组比较:

需要调用strcmp函数。

int strcmp(char *s1, char *s2);

按照ascii码比较,当s1和s2相等时返回0,如果s1大则返回1,否则返回-1。


2 å­—符数组赋值。

需要调用strcpy函数。


char *strcpy(char *dst, char *src);

将src中的字符串复制到dst中。


注意:要使用以上两个函数,需要引用头文件cstring。


以下是一个排序的参考代码:

#include <cstring>
#include <iostream>
using namespace std;
int main()
{
    char buf[100][100];//100个字符数组组成的二维数组。
    char t[100];
    int i,j;
    
    for(i = 0; i < 100; i ++)
        cin>>buf[i];//输入值。
    for(i = 0; i < 99; i ++)//执行排序。选择法。
        for(j = i+1; j<100; j ++)
        {
            if(strcmp(buf[i],buf[j]) < 0)//比较
            {
                strcpy(t,buf[i]);
                strcpy(buf[i],buf[j]);
                strcpy(buf[j], t);//这三句为交换。
            }
        }
     for(i = 0; i < 100; i ++)
         cout << buf[i]<<endl;//输出排序后的值。
         
     return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-24
#include<iostream>
#include<string>
using namespace std;

template <typename T>
void 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;}
}
}

//字符串数组的排序参考如下:

void sort(char str[][20], int n) //数组行数由n确定
{
    char a[20];
    int i, j;
    for (i = 0; i < n-1; i++) {
for(j=0;j<n-i-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);
}
}
}

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[d]<<"\t"; //s[d]才是输出字符串
}
cout<<"\n";

return 0;
}

追问

真是麻烦你们了,多谢!

追答

不客气

本回答被提问者采纳
第2个回答  2014-12-05
你好!
问题挺多的,我给你改了!
#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;
}追问

但还是有问题,对字符串数组排序还是出现错误could not deduce template argument for '' from 'void',问一下,怎样解决?

追答

我编译通过了啊而且运行没问题啊!你是编译错误还是运行时错误,哪一行的问题啊?

追问

void sort(char str[20][20], int n) {

这行出问题了

could not deduce template argument for '' from 'void',问一下,怎样解决?

追答

str[20][20]改为str[][20]即可。

追问

还是不行啊,

第3个回答  2014-12-06
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;

追问

麻烦了,多谢!

第4个回答  2014-12-05
我想问你为什么不使用STL子代的排序<algorithm>的sort函数或者qsort函数,追问

void sort(char str[20][20], int n) {

这行出问题了

追答#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];}
void sort(char str[][20], int n) {
    char a[20];
    int i, j, k;
    for (i = 0; i < n-1; i++) {
        k = i;
        for (j = i + 1 ; j < n; j++){/*here j should from i+1 not i*/
            if (strcmp(str[j], str[i]) < 0) 
                k = j;
            if (k != i)
            {
                strcpy(a, str[k]);
                strcpy(str[k], str[i]);
                strcpy(str[i], a);
            }
    }    
    }
}
int main()
{
   
    char s[][20]={"hsdfkjf","dhfjsdfs"};
    //const int q=sizeof(s) / sizeof(*s);
    sort(s,2);
    for(int d=0;d<2;d++)
    {cout<<s[d]<<"\t";}
    cout<<"\n";
    return 0;
}

追问

我用vc++6.0,运行一直出错!

追答

我是因为字数太多,黏贴不上,这些知识部分代码,其他的地方不用改,我运行过了。

相似回答