views:

642

answers:

3

I need to iterate over a queue. www.cplusplus.com says: By default, if no container class is specified for a particular queue class, the standard container class template deque is used.

So can I somehow get to the queue's underlying deque and iterate over it?

Thanks.

+7  A: 

If you need to iterate over a queue then you need something more than a queue. The point of the standard container adapters is to provide a minimal interface. If you need to do iteration as well, why not just use a deque (or list) instead?

Charles Bailey
While I know what you're saying, I've always disliked this phrasing "something more than a queue". A queue with enumeration is still a queue... Also, observe how `deque` just happens to support enumeration, completely arbitrarily. You could just as well argue that `deque` should be just as purist as `queue` and not support iteration, and if you want to iterate it then you want something "more"; e.g. a `deque_enumerable`. It's a slippery slope though, and my personal feeling is that `queue` should have supported enumeration in the first place.
romkyns
@romkyns: Would it be better if I rephrased it: "You need something with a richer interface than the `queue` interface so you should choose an object with a suitable interface". Like it or not, iteration isn't part of the `queue` interface so if you want iteration you need to choose something else.
Charles Bailey
A: 

In short: No There is a hack, use vector as underlaid container, so queue::front will return valid reference, convert it to pointer an iterate until <= queue::back

Dewfy
But you might as well use a vector directly instead...
graham.reeds
You can also directly use deque - that contains all necessary methods as queue but also supports iteration
Dewfy
+3  A: 

If you need to iterate a queue ... queue isn't the container you need.
Why did you pick a queue?
Why don't you take a container that you can iterate over?

TimW
I'm not the OP, but here are my answers, in case anyone is curious: 1) I picked a queue because I want a queue. I want to enqueue at one end and dequeue at the other. Is this not a reasonable choice? 2) It's not obvious that a "queue" is not enumerable, nor which structure to use instead. Your answer would have been more helpful if you explained which container to use instead.
romkyns