Pages

Boost heap priority queue

When the C++ STL priority queue is not enough, we could check if the Boost Heap priority queue is a better match to our requirements.

For instance, we could be interested in traversing the elements in the priority queue without popping them. Boost Heap priority queue provides a constant iterator that doesn't allow us to modify the underlying data, but lets us fetch them:
#include <iostream>
#include <boost/heap/priority_queue.hpp>

// ...

typedef boost::heap::priority_queue<int> MyPriQue;

MyPriQue pq;
pq.push(42);
pq.push(22);
pq.push(24);
pq.push(44);

for(MyPriQue::const_iterator it = pq.begin(); it != pq.end();++it)
{
    std::cout << *it << ' ';
}
std::cout << std::endl;
The output is:
44 42 24 22

No comments:

Post a Comment