by the stash, for the stash
Latest Reviews & Articles
Latest Topic Replies
|
|
Unfortunately for us C++ junkies, Bjarne's priority queue implementation is slightly lacking. If you don't want to jump ship and use the boost library, but still want to gain some additional priority queue functionality, you might be in luck. This small modification extends the priority queue and allows you to directly access the underlying container. This means, especially if you're using a vector, you have full freedom to iterate through your entire priority queue. This makes algorithms like the infamous A* pathfinder much simpler to implement.
template <class T, class S, class C> S& Container(std::priority_queue<T, S, C>& q) { struct HackedQueue : private std::priority_queue<T, S, C> { static S& Container(std::priority_queue<T, S, C>& q) { return q.*&HackedQueue::c; } }; return HackedQueue::Container(q); } int main() { std::priority_queue<SomeClass> pq; std::vector<SomeClass> &tasks = Container(pq); return 0; }