其他分享
首页 > 其他分享> > set自定义比较函数

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

 

标签:p2,set,return,函数,自定义,p1,const,first
来源: https://www.cnblogs.com/ydUESTC/p/16514072.html