set自定义比较函数

set的比较函数必须写成仿函数(class、struct、lambda)

而不能写成函数

且operator()的参数和函数都需要用const修饰

举例:

struct cmp
{
    bool operator()(const pair<int, string>& p1,const pair<int, string>& p2) const
    {
        if (p1.first > p2.first) return true;
        else if (p1.first < p2.first) return false;
        else
        {
            if (p1.second < p2.second) return true;
            else return false;
        }
    };
};

set<pair<int, string>, cmp> is;
View Code

 

posted @ 2022-07-24 10:53  80k  阅读(249)  评论(0)    收藏  举报