优先队列中实现随机点删除
priority_queue可以通过继承来自定义随机点删除(优先队列默认为最大堆)
template<typename T>
class custom_priority_queue : public std::priority_queue<T, std::vector<T>>
{
public:
bool remove(const T& value) {
auto it = std::find(this->c.begin(), this->c.end(), value);
if (it != this->c.end()) {
this->c.erase(it);
std::make_heap(this->c.begin(), this->c.end(), this->comp);
return true;
}
else return false;
}
};
可通过priority_queue中的protected成员c和comp来进行子类的继承引用。

浙公网安备 33010602011771号