下面两个我怎么不能通过编译啊?为什么

我在网上找了一些stl的sort函数的用法,有些用法怎么不能实现,请高手介绍一下sort的用法。
sort按降序排列如何实现?是 sort(A,1,'descend')吗?
如A是二维数组,如何实现按行排序,是sort(A,2),听说参数1是列排列,参数2是行排列。

ABOUTYOU
浏览 85回答 2
2回答

吃鸡游戏

使用sort()函数在做简单排序算法时候是非常好的方法。sort(buffer,buffer+n,cmp);&nbsp;buffer为待排序数组的首地址,buffer+n为待排序数组的最后一个数据的地址。cmp为自定义的排序规则函数,可省略。sort()函数默认是为升序排列,允许排序类型包括数值/字符/字符串。sort()也可以对结构体进行排序。cmp函数的返回值为true和false或1和0,若为true/1,则sort()函数为升序排列,若为false/0,则sort()函数为降序排列。下面为一个找出奶牛产奶量中间值的小程序,举例说明:#include "iostream"#include "algorithm"using namespace std;//奶牛结构类typedef struct&nbsp;&nbsp;{&nbsp;int milk;&nbsp;int num;}COW;COW cow[100];bool cmp(COW A, COW B);//主函数void main(){&nbsp;&nbsp;int n;&nbsp;cout<<"请输入奶牛的数量:&nbsp; ";&nbsp;cin>>n;&nbsp;for(int i=1;i<=n; i++)&nbsp;{&nbsp; cout<<"请输入奶牛"<<i<<"的产奶量:&nbsp; ";&nbsp; cin>>cow[i-1].milk;&nbsp; cow[i-1].num = i;&nbsp;}&nbsp;sort(cow,cow+n,cmp);&nbsp;//排序比较&nbsp;cout<<"中间奶牛产奶量为:&nbsp; "<<cow[n/2].milk<<endl;&nbsp;system("pause");}//cmp排序规则函数bool cmp(COW A, COW B){&nbsp;if (A.milk < B.milk)&nbsp; //按产奶量由小到大排序&nbsp;{&nbsp; return true;&nbsp;}&nbsp;else if (A.milk == B.milk)&nbsp;{&nbsp; if (A.num > B.num)&nbsp; //产奶量相同时,按序号由大到小排序&nbsp; {&nbsp; &nbsp;return true;&nbsp; }&nbsp; return false;&nbsp;}&nbsp;else&nbsp;{&nbsp; return false;&nbsp;}}&nbsp;

慕容森

#include <algorithm>void sort( iterator start, iterator end );void sort( iterator start, iterator end, StrictWeakOrdering cmp );
打开App,查看更多内容
随时随地看视频慕课网APP