新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

c++STL模板库的set和multiset自定义排序函数-创新互联

c++ STL模板库的set和multiset自定义排序函数

以set具体说明

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了峡江免费建站欢迎大家使用!方法一(强烈推荐写法)
#include#includeusing namespace std;

struct node
{int x;
    int y;
    
    bool operator<(const node& a) const
    {if (a.x == x)
            return y< a.y;
        return x< a.x;
    }
};
//在 set 里面进行重载,和优先队列一样

void run_case()
{sets;
    s.insert(node{1, 1 });
    s.insert(node{1, 1 });
    s.insert(node{1, 2 });
    s.insert(node{1, 3 });
    s.insert(node{2, 2 });
    s.insert(node{2, 3 });

    cout<< s.begin()->x<< " "<< s.begin()->y<< "\n"; // 输出 1,1
    
}

int main()
{run_case();
	return 0;
}
方法二(一般推荐)
#include#includeusing namespace std;

struct node
{int x;
    int y;
};

class comp
{public:
    bool operator()(const node& a, const node& b) const
    {if (a.x == b.x)
            return a.y< b.y;
        return a.x< b.x;
    }
   
};

void run_case()
{sets;
    s.insert(node{1, 1 });
    s.insert(node{1, 1 });
    s.insert(node{1, 2 });
    s.insert(node{1, 3 });
    s.insert(node{2, 2 });
    s.insert(node{2, 3 });
    cout<< s.begin()->x<< " "<< s.begin()->y<< "\n";
}

int main()
{run_case();
	return 0;
}
方法三(了解就好)
#include#includeusing namespace std;

struct node
{int x;
    int y;
};


bool comp(const node& a, const node& b)
{if (a.x == b.x)
        return a.y< b.y;
    return a.x< b.x;
}

void run_case()
{sets(comp);
    s.insert(node{1, 1 });
    s.insert(node{1, 1 });
    s.insert(node{1, 2 });
    s.insert(node{1, 3 });
    s.insert(node{2, 2 });
    s.insert(node{2, 3 });

    cout<< s.begin()->x<< " "<< s.begin()->y<< "\n";
}

int main()
{run_case();
	return 0;
}

对于multiset也是一样的用法,可以自行尝试

参考资料点这里

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


新闻标题:c++STL模板库的set和multiset自定义排序函数-创新互联
分享地址:http://www.wjwzjz.com/article/dehgdg.html
在线咨询
服务热线
服务热线:028-86922220
TOP