C++对字符数组排序

2025-02-23 18:21:11
推荐回答(5个)
回答1:

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

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

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 
#include 
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]<         
     return 0;
}

回答2:

#include
#include
using namespace std;

template 
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            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 {
cout< }
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 {
cout< }
cout<<"\n";

char c[]={'a','b','y','u','r'};
const  int   p=sizeof(c) / sizeof(*c);
sort(c,p);
for(int k=0;k {
cout< }
cout<<"\n";
//以上均调用模板函数
char *s[]={"hsdfkjf","dhfjsdfs"} ; //定义指针数组
const int q=sizeof(s) / sizeof(*s);
sort(s,q);//调用字符串排序函数
for(int d=0;d {
cout< }
cout<<"\n";

return 0;
}

回答3:

你好!
问题挺多的,我给你改了!
#include
#include
#include "string.h"
using namespace std;

template
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 {cout< 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 {cout< cout<<"\n";

char c[]={'a','b','y','u','r'};
const int p=sizeof(c) / sizeof(*c);
sort(c,p);
for(int k=0;k {cout< cout<<"\n";

char s[][20]={"hsdfkjf","dhfjsdfs"};
const int q=sizeof(s) / sizeof(*s);
sort(s,q);
for(int d=0;d {cout< cout<<"\n";

return 0;
}

回答4:

	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 {
cout << s[d] << "\t";
}
cout << "\n";

return 0;

回答5:

我想问你为什么不使用STL子代的排序的sort函数或者qsort函数,