Still priority queues and STL ...

D

Der Andere

I posted this reaction in a thread quite far below so I fear people won't
read it. I got stuck now for quite a while and just don't know what to try
else.
Thanks,
Matthias
See template class priority_queue in the header <queue>. You'll
have to supply an ordering predicate that takes two pointers
and returns true when one of the designated objects is ordered
before the other, but that shouldn't be hard.

The following sample program does not compile but I have no idea what to do
about it. Might be a beginner's fault. If I substitute pv(comp()) with
pv(std::less<Vertex*>()) the compiler gives no error, but I strongly suppose
less() would order the elements according to the address value of the
pointer.

#include <queue>

struct Vertex
{
int a;
};

bool comp (Vertex* v1, Vertex* v2)
{
return v1->a < v2->a;
}

int main()
{
std::priority_queue<Vertex*> pv(comp());
return 0;
}

Regards,
Matthias
 
D

David Harmon

On Fri, 4 Jun 2004 18:09:39 +0200 in comp.lang.c++, "Der Andere"
The following sample program does not compile but I have no idea what to do
about it. Might be a beginner's fault. If I substitute pv(comp()) with
pv(std::less<Vertex*>()) the compiler gives no error,
std::priority_queue<Vertex*> pv(comp());

1. You do not want to pass to the constructor the result of calling
comp() but rather the address of the function &comp.

2. The type of the comparator is a template parameter of
priority_queue, so if you want to use something other than the default
std::less you must spell it all out.

std::priority_queue<Vertex*, std::vector<Vertex *>,
bool(*)(Vertex *, Vertex *) > pv(&comp);
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top