reviewstash

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

Strict Standards: Only variables should be passed by reference in /home/rvsadmin/reviewstash.com/syn/phpMSH/classes.php on line 191

#1 11/25/11 11:16 AM

Daniel Levy's Avatar
Daniel Levy United States
Staff
Gender: male
From: Sacramento / Santa Barbara, CA
Registered: 12/08/07
Posts: 6976
Karma: 77
Note: UTF Spokesman
Reputation :   153 

Website

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.

Code:

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