sort注意事项

sort(a, a + 10, cmp); 注意若a应该是第一个数据的位置。a+10为最后一个数据的指标;

#include<iostream>
#include<cmath>

using namespace std;

#include <cmath>
#include <algorithm>

bool cmp(int a, int b) {
    if (a > b) return 1;
    return 0;
    //不能有等号。
}
//sort默认是以小于符号进行比较的排序的;
//cmp函数加入sorti替代<符号, 
//表示:当a>b 时候(事实上),则sort函数中a<b返回值为1,为真。
int main() {
    int a[10] = {1, 6, 9, 4, 3, 8, 77, 65, 12, 0};
    sort(a, a + 10, cmp);
    //
    for (int i = 0; i < 10; i++) {
        printf("%d ", a[i]);
    }
    return 0;
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务