by the stash, for the stash

Guest Information

Reviewstash isn't just another technology review website - it's your review website! Registered members can submit reviews to be featured on the home page, participate in contests, and ask questions in the forums - register today to get started! Registration also removes interstitial ads.
  • Forum
  •  » News
  •  » Accessing an STL Priority Queue's Container
Topic rating: 0

#1 11/25/11 11:16 AM

sdm's Avatar
sdm United States
Gender: male
From: California
Registered: 12/08/07
Posts: 7703
Karma: 85
Note: Border Zoned
Reputation :   173 


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;

11/25/11 11:16 AM

Advertisement Bot

  • Forum
  •  » News
  •  » Accessing an STL Priority Queue's Container

Board footer

Optimized for 1280x1024 @ 32 bit
Fueled by FluxBB
Top 20 Posts
Who's Online
Index Map
About Us