sort的使用

C++中Sort使用

头文件是#include <algorithm>;

sort的使用

#include<iostream>
#include<algorithm>

using namespace std;

bool cmp(int x, int y){

    return x > y;
}
int main(){

    int a[5] = { 1, 99, 5, 23, 67 };
    sort(a/*排序的数组的起始地址*/, a + 5/*结束的地址(最后一位要排序的地址)*/,cmp/*排序的方法,可以是从大到小也可是从小到大,还可以不写第三个参数,此时默认的排序方法是从小到大排序*/);
    for (int i = 0; i < 5; i++){
        cout << a[i] << " ";
    }
    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/pcdl/p/12312359.html