min heap (priority queue)

I

imutate

How do you use std::priority_queue to store say an index type (call it
intidx) and sort using another integer type (but smallest weight at the
top) ? How do you pop off the top index ? I mean top() returns the
top element but how do you access the index and the weight.
Is it std::priority_queue <intidx, int>; ? Where does the comparison
fit into it ?
 
R

red floyd

How do you use std::priority_queue to store say an index type (call it
intidx) and sort using another integer type (but smallest weight at the
top) ? How do you pop off the top index ? I mean top() returns the
top element but how do you access the index and the weight.
Is it std::priority_queue <intidx, int>; ? Where does the comparison
fit into it ?
Use a priority queue of std::pair<int int>.
 
R

Rolf Magnus

How do you use std::priority_queue to store say an index type (call it
intidx) and sort using another integer type (but smallest weight at the
top) ?

Define a struct of everything you want to store and use that as element
type. Then define an appropriate operator< for that struct.
 
G

Greg

Rolf said:
Define a struct of everything you want to store and use that as element
type. Then define an appropriate operator< for that struct.

OK
struct helt {
int intidx;
}

std::priority_queue<

...but then what ?
 
R

red floyd

Greg said:
Why would you use a pair ? That is not right for a heap.

Why not? He want a heap of two things: an index and an integer weight.
Define an operator< which sorts the pairs by weight, and you are fine
with a heap of pairs.
 
G

Greg

Use a priority queue of std::pair said:
Why not? He want a heap of two things: an index and an integer weight.
Define an operator< which sorts the pairs by weight, and you are fine
with a heap of pairs.

I forgot my terminology I am talking about a queue that is implemented
as a heap. I don't need a deque (double ended queue, but knowing how
to implement one is good.)

Yes, i want to implement an equivalent to your definition, but I've
never heard of it being implemented like this. Elements in a heap or a
priority queue are weighted by definition.

Here is an example I find a bit confusing because using a vector does
not make sense (a vector is not a heap, although admitidely it could
store a queue)

std::priority_queue<int,
std::vector<int, std::allocator<int> >,
std::less<int> > pq;

This looks more promising...

std::priority_queue<int,
std::deque<int, std::allocator<int> >,
std::less<int> > pq;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top