迭代和修改c++优先级队列
Iterate and modify c++ priority queue
我想要一个可以迭代和修改的优先级队列。也就是说,非常量迭代器。
似乎std::priority_queue<T>
不支持修改键值以保护结构的损坏。
我发现boost::heap::prioity_queue
支持键的可变性,priority_queue/用update函数修改值以保持数据结构的不变性。
boost也支持迭代器,但它们是常量迭代器,不允许修改。
该标准具有帮助维护堆、make_heap
、push_heap
和pop_heap
的函数。将它们与std::vector
或其他随机访问容器结合使用,维护一个可以迭代的堆并不需要太多的工作。
的例子:
std::vector<int> heap = {1,3,5,7,9};
std::make_heap(heap.begin(), heap.end());
// add an element
heap.push_back(4);
std::push_heap(heap.begin(), heap.end());
// remove an element
std::pop_heap(heap.begin(), heap.end());
heap.pop_back();
// multiply each element by 2
for (auto & i : heap)
i *= 2;
演示还要注意,所有堆函数都有一个比较器的可选参数,以便您可以控制堆的排序。
相关文章:
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 按对象的特定方法按升序排列的C++优先级队列
- 使用2个键的cpp-stl::优先级队列排序不正确
- 为什么我需要C++中不同的排序格式来对这个USACO代码上的数组和优先级队列进行排序
- 打印优先级队列
- 带自定义比较器的最小优先级队列
- 优先级队列自定义比较器
- 排序数组优先级队列
- 更改运行时优先级队列的排序功能
- 优先级队列构造函数的工作
- 实现优先级队列
- 优先级队列功能比较
- 在C++中打印对的优先级队列的所有值时出现问题
- 使用堆的优先级队列,具有相同键的值不遵循 FIFO(先进先出)
- 为什么某些 STL 容器(堆栈、队列、优先级队列)不支持迭代器?
- 是否可以使用简单队列创建优先级队列
- 如何在 c++ 中创建对的优先级队列.这会弹出具有最小值的元素.默认的弹出最大值
- Cython中带有自定义比较器的优先级队列
- 优先级队列比较器[C++].
- asio::io_service 具有多个线程的优先级队列处理