stl的优先级队列

  1. 云栖社区>
  2. 博客>
  3. 正文

stl的优先级队列

石门-archy 2016-04-20 17:05:17 浏览721
展开阅读全文

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

class Timer;

typedef Timer* RTimer;


class Timer
{
public:
    Timer():_interval(0),_expires_time(0){}

    virtual ~Timer(){}

    virtual void schedule_timer(int sec,int usec = 0)
    {

    }

    virtual long expires_time()
    {
        return this->_expires_time;
    }


    virtual long interval_time()
    {
        return this->_interval;
    }

public:

    long _interval;

    long _expires_time;

};


struct RTimerCmp
{
    bool operator()(RTimer left,RTimer right)
    {
        if(left->_expires_time == right->_expires_time)
        {
            return right->_expires_time > left->_expires_time;
        }
        else
        {
            return left->_expires_time > right->_expires_time;
        }
    }
};


std::priority_queue<RTimer,std::vector<RTimer>,RTimerCmp > _rtime_queue;

int main()
{
    Timer timer1;
    timer1._expires_time = 1;

    Timer timer2;
    timer2._expires_time = 2;

    Timer timer3;
    timer3._expires_time = 3;

    _rtime_queue.push(&timer1);
    _rtime_queue.push(&timer3);
    _rtime_queue.push(&timer2);

    while(true)
    {
        if(_rtime_queue.size() <= 0)
        {
            break;
        }
        RTimer r = _rtime_queue.top();
        cout << "expire " << r->_expires_time << endl;
        _rtime_queue.pop();
    }

    return 0;
}


:-),记录一下!

stl 容器 优先级队列 指针

网友评论

登录后评论
0/500
评论
石门-archy
+ 关注