Does anyone know why std::queue, std::stack, and std::priority_queue don’t provide a clear() member function? I have to fake one like this:
std::queue<int> q; // time passes... q = std::queue<int>(); // equivalent to clear()
IIRC, clear() is provided by everything that could serve as the underlying container. Is there a good reason to not have the container adaptors provide it?
Well, I think this is because
clearwas not considered a valid operation on a queue, a priority_queue or a stack (by the way, deque is not and adaptor but a container).So when using a queue, all you can do is push/pop elements; clearing the queue can be seen as a violation of the FIFO concept. Consequently, if you need to clear your queue, maybe it’s not really a queue and you should better use a deque.
However, this conception of things is a little narrow-minded, and I think clearing the queue as you do is fair enough.