常用STL

· · 个人记录

排序:

sort(a+x, a+x+n, cmp);

a为要排序的数组名,x为数组内容的起始下标,n为数组数的个数,cmp是函数名,内容为数组中数的优先级,cmp可以省略,默认为从小到大排序。

bool cmp(const int& x, const int& y) { return x > y; }

上面这个cmp就是从大到小排序。

快选:

nth_element(a+x, a+x+k, a+x+n, cmp);

a为数组名,x为数组内容的起始下标,k表示将数组排序后第k个数归位,n是数组的长度。cmp和sort的一样。需要注意的是,nth_element并不会将数组真的排序。

2022.10.20

#include <bits/stdc++.h>

using namespace std;

struct Node
{
    int a, b;
    bool operator <(const Node& y) const // !!!
    {
        if (a != y.a) return a < y.a;
        return b < y.a;
    }
}x, y;

int main()
{
    max(x, y);
    return 0;
}

那个const去掉会编译错误。