std::priority_queue
场景:
1. 对于一个任务队列,任务的优先级由任务的priority属性指明,这时候就需要优先级越高的先执行。而queue并没有排序功能,这时priority_queue是比较好的选择.
2 对于异步的task也是一样,在不断添加新的task时,当然希望优先级越高的先执行.
解析:
1. 如果需要把优先级最高的先pop,那么comp比较时需要返回false.
代码:
//1.Elements are popped from the "back" of the specific container,
//which is known as the top of the priority queue.
//2.shall return true if a is considered to go before b
// in the strict weak ordering the function defines
#include <stdlib.h>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
class Task
{
public:
Task(int priority):priority_(priority)
{
}
~Task(){}
int priority_;
void DoW

本文介绍了C++中std::priority_queue的使用,特别是在任务优先级管理和异步任务调度中的作用。通过实例代码展示如何根据任务的priority属性实现优先级执行,并说明了comp比较函数的配置方法。
订阅专栏 解锁全文
4093

被折叠的 条评论
为什么被折叠?



