/*1.
------------------------------------------
sort 对字符型排序
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int cmp(char a, char b){
return a > b;
}
int main (){
char a[10];
scanf("%s", a);
sort(a, a + 3,cmp);
printf("%s\n", a);
return 0;
}
------------------------------------------
*/
/*2.
------------------------------------------
sort对int型 排序
# include<stdio.h>
# include<string.h>
# include<algorithm>
using namespace std;
int main()
{
int a[3];
int i,t;
for(i=0;i<3;i++) scanf("%d",&a[i]);
sort(a,a+3);
for(i=0;i<3;i++) printf("%d",a[i]);
}
-------------------------------------------
*/
/*3.
-------------------------------------------
sort对浮点型排序
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std;
int m,n,t,i;
int cmp(double a,double b){
return a>b;
}
int main(){
double a[5];
for(i=0;i<5;i++) scanf("%lf",&a[i]);
sort(a,a+5,cmp);
for(i=0;i<5;i++) printf("%lf\n",a[i]);
return 0;
}
-------------------------------------------
*/
/* 4.
-------------------------------------------
sort对字符型数组排序
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std;
struct str
{
int a;
char c[5];
}b[5];
int cmp(str x,str y)
{
return strcmp(x.c,y.c)<0;
}
int main(){
int m,n,t,i,s;
scanf("%d",&m);
for(t=0;t<m;t++)
scanf("%s",b[t].c);
sort(b,b+m,cmp);
for(t=0;t<m;t++)
printf("%s\n",b[t].c);
return 0;
}
--------------------------------------------
*/
个人对sort几种用法的总结
最新推荐文章于 2021-04-03 17:39:05 发布