views:

90

answers:

2

Why wouldn't C++ allow something like that ?

I need to have multiple priority queues, the number of which would be determined at run time.

This fails to compile

std::vector<std::priorityqueue<Class A>>.

Is there a better approach ?

+5  A: 

The correct code would be:

std::vector<std::priority_queue<A> >

Note that Class does not belong next to A, priority_queue has an underscore in it, and there is a space required between the two right angle brackets (>> is parsed as a right shift operator).

This also requires that A is less-than comparable (if it is not, then you must provide a comparison function to be used by the priority queue).

James McNellis
i know about the class and the underscore thing. didnt know about the space thing. Probably why it was acting up.
cyrux
`std::priority_queue<Block_Details>* free_list; `is this legal ?
cyrux
@cyrux: Yes, but why do you want a pointer to `priority_queue`?
James McNellis
Not a pointer, more like an array of priority queues
cyrux
@cyrux: That's why you have them in a `vector`...
James McNellis
Coming to think about it , i dont think arrays of queues are possible. Arrays are supposed to have starting and ending addresses and queues are supposed to grow. this would make it illegal ?
cyrux
@cyrux: Do you know what `vector` and `priority_queue` are and how they function? If not, you might consult your C++ book or Google (they are well documented in the SGI STL documentation).
James McNellis
what part of my comment didnt make sense ?
cyrux
@cyrux, Objects you create with `new` or `new[]` do not change at runtime. A `vector` has a pointer to a chunk of memory which (perhaps) changes in size with the size of the array. The `vector` is storing just the pointer, and the size of a pointer doesn't change at runtime, so the object doesn't change size. So yes, an array of `priority_queue`s is legal and valid, but you're probably better off using a `vector` or `list` anyway.
strager
A: 

This should work just fine. Just the syntax should be:

std::vector<std::priority_queue<A> >

(note the space (" ") near the end.

jpalecek
This is only needed if you have a crappy compiler not able to differenciate the operator `>>` and ends of cascaded templates (e.g. MSVC 6.0). This should be no problem for any decent C++ compiler nowadays.
jdehaan
Oh, so it was treating `>>` as right shift, wow. I have Netbeans on linux btw
cyrux
@jde: The C++03 language simply doesn't support this, only C++0x will fix it.
Georg Fritzsche
@jdehaan: Wrong. Any strictly C++03 compliant compiler won't accept this.
Axel Gneiting
Thanks Georg, Axel, I thought it was only a matter of compilers. Good to know that the new C++ standard enforces this to work. Looked to me the compilers got better at parsing.
jdehaan