c++ - 检查新添加的 unique_ptr 到 priority_queue 的位置
问题描述
我正在切换我的设计以使用智能指针,我遇到了 std::priority_que 的问题
我有一种方法可以将新任务插入队列,并在新任务到达队列顶部时发出信号:
bool foo(SchedulerTask* task)
{
eventList_.push(task);
return task == eventList_.top();
}
将 SchedulerTask 包装到 unique_ptr 后,我遇到了在容器中检查其优先级的问题。将对象移入队列后,我无法再次使用它进行比较。我最终缓存了比较器成员并将其与顶部对象进行比较:
bool foo(std::unique_ptr<SchedulerTask> task)
{
const auto prio = task->getCycle(); // my priority_queue compares objects by getCycle() value
eventList_.push(std::move(task));
return prio >= eventList_.top()->getCycle();;
}
可以做得更好吗?
解决方案
正如@Sam Varschavchik 暗示的那样,您可以与原始指针进行比较。即你会做类似的事情:
bool foo (std::unique_ptr<SchedulerTask> task) {
auto const * taskPtr = task.get();
eventList_.push(std::move(task));
return taskPtr == eventList_.top().get();
}
推荐阅读
- node.js - B1 类的 GC App Engine 部署是否会闲置,即使它们至少有 1 个实例?
- python - 将加载的变量作为参数而不是文件路径python传递
- mongodb - 如何在 MongoDB Compass 中显示图像
- python - 如何将相同的值追加到列中,直到达到最大行?
- apache-kafka - 我无法设置我的环境来将我的消费者连接到 docker kafka
- c# - ColumnHeaderStyle 在将其添加到 DataGridStyle 时不起作用
- r - 将 Lubridate 周期列表转换为周期向量
- email - Office365 允许第三方客户端的欺骗电子邮件
- c++ - 如何在 ceres 求解器中使用三次插值
- oracle - 如何根据输入变量运行不同的查询?