priority_queue+Windows Message Queue

好坑的一道题!!!wrong了三遍,才ac!
重要的时间说三遍,细节细节细节
题目链接
涉及的知识点:优先队列的插入,判空,输出,以及自定义结构体优先级(重点)
在看以下代码之前,还请自己做一遍,一定要注意时间和空间哦!!

#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
struct student
{
    char place[110];//一开始开到了10000,超空间了
    int x;
    int y;
    int id;
    friend bool operator < (student a,student b)
    {
        if(a.y==b.y) return a.id>b.id;
        else return a.y>b.y;//y小的在前;
    }
};
int main()
{
    char order[4];//一开始用的string,超时了
    priority_queue<student>s;
    int k=0;
    while(scanf("%s",order)!=EOF)
    {
        if(order[0]=='G')
        {
            if(s.empty())
                printf("EMPTY QUEUE!\n");
            else
            {
                printf("%s %d\n",s.top().place,s.top().x);
                s.pop();
            }

        }
        else if(order[0]=='P')
        {
            student note;
            scanf("%s%d%d",note.place,&note.x,&note.y);
            note.id=k++;
            s.push(note);
        }
    }
    return 0;
}

没有百度到为什么用string会超时的愿因,先记住吧,能用char 就用char吧!!
这里说一下对重载小于号进行一下解释:

//通过一个元素来确定关系;
struct student
{
    int y;
    friend bool operator < (student a,student b)
    {
   	return 	a.y>b.y;//y小的在前面;
   	//可以这样理解:
   	//【b】【a】
   	//b在a的前面,a.y>b.y时返回1,所以小的在前面;
	//return a.y<b.y;//y大的在前面;
    }
};
//通过两个元素来确定关系;
struct student
{
    int y;
    int id;
    friend bool operator < (student a,student b)
    {
        if(a.y==b.y) return a.id>b.id;//当y相同时,id小的在前面;
        else return a.y>b.y;//y小的在前;//y不同时,小的在前面;
    }
};

在STL里涉及到结构体时,有许多情况需要重载,上面介绍的重载小于号,还是冰山一角,自己还可以多多了解。

转载的一篇很好的文章

using clock_type = std::chrono::system_clock; struct message { clock_type::time_point when; std::function<void()> callback; std::string param; }; class message_loop { public: message_loop(): _stop(false) { // } message_loop(const message_loop&) = delete; message_loop& operator=(const message_loop&) = delete; void run() { while (!_stop) { auto msg = wait_one(); msg.callback(); } } void quit() { post({clock_type::now(), this{ _stop = true; } }); } void post(std::function<void()> callable) { post({clock_type::now(), std::move(callable)}); } void post(std::function<void()> callable, std::chrono::milliseconds delay) { post({clock_type::now() + delay, std::move(callable)}); } private: struct msg_prio_comp { inline bool operator() (const message& a, const message& b) { return a.when > b.when; } }; using queue_type = std::priority_queue<message, std::vector<message>, msg_prio_comp>; std::mutex _mtx; std::condition_variable _cv; queue_type _msgs; bool _stop; void post(message msg) { auto lck = acquire_lock(); _msgs.emplace(std::move(msg)); _cv.notify_one(); } std::unique_lockstd::mutex acquire_lock() { return std::unique_lockstd::mutex(_mtx); } bool idle() const { return _msgs.empty(); } const message& top() const { return _msgs.top(); } message pop() { auto msg = top(); _msgs.pop(); return msg; } message wait_one() { while (true) { auto lck = acquire_lock(); if (idle()) _cv.wait(lck); else if (top().when <= clock_type::now()) return pop(); else { _cv.wait_until(lck, top().when); // 可能是新消息到达,再循环一次看看 } } } }; int main(int argc, char *argv[]) { using namespace std; using namespace std::chrono; message_loop *pLoop = new message_loop; thread th(pLoop{ pLoop->run(); }); cout << "POST 1"<<endl;; pLoop->post({ cout << "1"<<endl; }); cout << "POST 2"<<endl;; pLoop->post({ cout << "2"<<endl; }, milliseconds(500)); cout << "POST 3"<<endl;; pLoop->post({ cout << "3"<<endl; }); cout << "POST 4"<<endl;; pLoop->post({ cout << "4"<<endl; }, milliseconds(1000)); this_thread::sleep_for(milliseconds(1500)); // pLoop->quit(); cout << "Quit"<<endl; th.join(); cout << "here"<<endl; } 请优化一下,可以传参
最新发布
05-30
可以将 message 结构体中的 param 字段用于传递参数,并相应地修改 post 函数和 wait_one 函数。具体代码如下: ``` using clock_type = std::chrono::system_clock; struct message { clock_type::time_point when; std::function<void(const std::string&)> callback; std::string param; }; class message_loop { public: message_loop() : _stop(false) {} message_loop(const message_loop&) = delete; message_loop& operator=(const message_loop&) = delete; void run() { while (!_stop) { auto msg = wait_one(); msg.callback(msg.param); } } void quit() { post({clock_type::now(), [](const std::string&) {}}, ""); _stop = true; } void post(std::function<void(const std::string&)> callable, const std::string& param) { post({clock_type::now(), std::move(callable), param}); } void post(std::function<void(const std::string&)> callable, std::chrono::milliseconds delay, const std::string& param) { post({clock_type::now() + delay, std::move(callable), param}); } private: struct msg_prio_comp { inline bool operator() (const message& a, const message& b) { return a.when > b.when; } }; using queue_type = std::priority_queue<message, std::vector<message>, msg_prio_comp>; std::mutex _mtx; std::condition_variable _cv; queue_type _msgs; bool _stop; void post(message msg) { auto lck = acquire_lock(); _msgs.emplace(std::move(msg)); _cv.notify_one(); } std::unique_lock<std::mutex> acquire_lock() { return std::unique_lock<std::mutex>(_mtx); } bool idle() const { return _msgs.empty(); } const message& top() const { return _msgs.top(); } message pop() { auto msg = top(); _msgs.pop(); return msg; } message wait_one() { while (true) { auto lck = acquire_lock(); if (idle()) { _cv.wait(lck); } else if (top().when <= clock_type::now()) { auto msg = pop(); return msg; } else { _cv.wait_until(lck, top().when); // 可能是新消息到达,再循环一次看看 } } } }; int main(int argc, char *argv[]) { using namespace std; using namespace std::chrono; message_loop *pLoop = new message_loop; thread th(pLoop, &message_loop::run); pLoop->post([](const std::string& param) { cout << "1 " << param << endl; }, "hello"); pLoop->post([](const std::string& param) { cout << "2 " << param << endl; }, milliseconds(500), "world"); pLoop->post([](const std::string& param) { cout << "3 " << param << endl; }, "foo"); pLoop->post([](const std::string& param) { cout << "4 " << param << endl; }, milliseconds(1000), "bar"); this_thread::sleep_for(milliseconds(1500)); pLoop->quit(); th.join(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值