首页 > 编程知识 正文

sort函数compare,c++sort函数用法

时间:2023-05-04 11:14:19 阅读:114322 作者:3134

一、个人资料1、头文件: #includealgorithm

2、时间复杂度:与快排相似,nlog(2) n,效率高

3、sort函数有三个参数。

(1)对数组的起始地址进行排序

2 )对数组中最后一个数据元素的下一个地址进行排序

)3)排序方法,如果没有排序方法,默认按从小到大的顺序排序

二、实例1、sort函数实现数的排序

# include cstdio # include iostream # includecstdlib # includealgorithmusingnamespacestd; BOLCMP(inta,int b )//按从大到小排序的比较函数({ return ab; (}int main ) ) { int i,j; intarr [ 10 ]={ 1,33,56,74,34,34,555,43,43,343 }; sort(arr、arr 10、cmp ); for(I=0; i10; I ) coutarr[i]' 'endl; 返回0; } 2、sort函数实现字符串排序(长度、字母顺序两种情况) ) ) )。

(1) sort函数对string中的字符进行排序(string是字符串型数据) ) ) ) )。

# include iostream # include cstdio # include algorithm # includecstringusingnamespacestd; OOLCMP(chara,char b )//比较函数) { return ab; (}int main ) ) { string s; cins; sort(s.Begin )、s.end ); //从小到大coutsendl; sort(s.Begin )、s.end )、cmp ); //从大到小coutsendl; couts.length () endl; //字符串长度函数(2)对sort一维字符数组中的字符进行排序

# include cstdio # include iostream # include cstring # includealgorithmusingnamespacestd; OOLCMP(chara,char b )//比较函数) { return ab; (}int main ) ) { char a[100]; 扫描(' % s ',a ); sort(a,a strlen(a ) a ); //从小到大coutaendl; sort(a,a strlen(a ) a ),cmp ); //从大到小coutaendl; coutstrlen(a ) endl; //输出字符数组a的长度}(3) sort按词典顺序对string型数组进行排序

# include cstdio # include iostream # includecstdlib # include algorithm # includecstringusingnamespacestd; BOLCMP(stringa,string b )//按词典顺序从大到小排序({ return ab; (}int main ) ) { int i,j; string s[12]={ 'January ',' February ',' March ',' April ',' May ',' June ',' August ',' septembbbbbby //字符串默认按词典顺序从小到大排序,数量默认按从小到大排序for(I=0; i12; I ) couts[i]endl; coutendl; sort(s,s 12,cmp ); //按词典顺序从大到小排序for (I=0; i12; I ) couts[i]endl; 返回0; (4) sort将char型的二维数组按词典顺序排序

错误,不能这样做。 也不能根据字符串的长度进行排序

解决方法为: 1、使用qsort 2,切换为字符串

)5) sort根据长度对string型的数组进行排序

# include cstdio # include iostream # includecstdlib # include algorithm # includecstringusingnamespacestd; boolcmP1(stringa,stringb(//按长度从小到大排序({ return a.length ) ) b.length ); }boolCMP2(stringa,stringb(//按长度从大到小排序({ return a.length ) ) b.length ); (}int main ) ) { int i,j; string s[12]={ 'January ',' February ',' March ',' April ',' May ',' June ',' August ',' septembbbbbby //按照长度顺序从小到大的顺序for(I=0; i12; I ) couts[i]endl; coutendl; sort(s,s 12,cmp2; //按长度从大到小排序for (I=0; i12; I ) couts[i]endl; 返回0; ) 6、对结构体数组进行排序(根据结构体中的某个要素) () ) ) ) ) ) ) ) ) ) ) ) )

# include cstdio # include iostream # include cstring # includealgorithmusingnamespacestd; 常数int n=3; 结构稳定{ char name [ 20 ]; //名称char id[20]; //学校编号int score; //成绩int r; //排名}stu[100010] /*(1)如果两个学生的分数不同,分数高的排名靠前)2)否则,将名字词典排名低的排名靠前()/boolcmp ) Student b,studedent=b.score(returna.scoreb.score; else返回流(b.name,b.name ) 0; (/*实现排名,一般规则是分数不同、排名不同、分数相同但占有相同的排名。 例如,分数为90、88、88、88、86; 等级为1,2,2,2,5 */void rank () { int i,j; stu[0].r=1; for(I=1; in; I () if ) stu[I].score==stu[I-1].score ) stu[i].r=stu[i-1].r; else stu[i].r=i 1; }}int main () { int i,j; stu[0]={'guo ',' 001 ',82,3 }; //这样分配值是错误的。 算了,以后再更改吧。 stu [1]={ '张',' 002 ',82,3 }; stu[2]={'wu ',' 003 ',99,2 }; sort(stu,stu n,cmp ); for(I=0; in; I ) coutstu[i]endl; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。