首页 > 编程语言 > 详细

C++重载运算符 < priority_queue 相关(个人笔记)

时间:2014-09-19 12:01:05      阅读:245      评论:0      收藏:0      [点我收藏+]

本来想用 priority_queue 去写个bfs。结果重载运算符忘了。ORZ。


然后看书和问别人熟悉了一下,记录一下。


struct lx
{
    int x,y,lv;
};

有一个这样的结构体。x,y,是坐标,lv 是它的权。重载<。

struct lx
{
    int x,y,lv;
    friend bool operator<(lx a,lx b)
    {
        return a.lv>b.lv;
    }
};

friend !友员

或者

struct lx
{
    int x,y,lv;
    bool operator < (lx a)const
    {
        return lv>a.lv;
    }
};

要通过STL 必须加上const 修饰。


然后就可以直接

priority_queue<lx> q;

或者不写重载,自己定义priority_queue的Compare函数

struct cmp
{
    bool operator()(lx a,lx b)
    {
        return a.lv<b.lv;
    }
};

priority_queue<lx,vector<lx>,cmp>q;

感觉还是重载了。。。。就这样吧,马上去用priority_queue 去优化一下bfs。



C++重载运算符 < priority_queue 相关(个人笔记)

原文:http://blog.csdn.net/dongshimou/article/details/39395373

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!