小白书79页对字符串和字符的排序

本文通过一个具体的程序实例对比了C++中qsort与sort函数的应用。该程序实现了对字符串数组进行排序,并对每个字符串内的字符进行单独排序。尽管作者尝试将qsort替换为sort但未成功,该示例仍展示了qsort的强大功能。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
int n;
char word[2000][10],sorted[2000][10];
int  cmp_char(const void* _a,const void* _b)
{
char* a=(char*)_a;
char* b=(char*)_b;
return *a-*b;
}
int  cmp_string(const void* _a,const void* _b)
{
char* a=(char*)_a;
char* b=(char*)_b;
return strcmp(a,b);
}
int main()
{
n=0;
for(;;)
{
scanf("%s",word[n]);
if(word[n][0]=='*')
break;
n++;
}
qsort(word,n,sizeof(word[0]),cmp_string);
for(int i=0;i<n;i++)
{
strcpy(sorted[i],word[i]);
qsort(sorted[i],strlen(sorted[i]),sizeof(char),cmp_char);
}
char s[10];
while(~scanf("%s",s))
{
qsort(s,strlen(s),sizeof(char),cmp_char);
int found=0;
for(int i=0;i<n;i++)
if(strcmp(sorted[i],s)==0)
{
found=1;
printf("%s ",word[i]);
}
if(!found)  printf(":(");
printf("\n");
}
return 0;

}

关于sort函数以及qsort函数的具体差别网上博客说的也不是很清楚,此题本想改成sort的用法,能力不够,改不对,因此还是用qsort

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值