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?
A container adaptor is not a container at all, since it doesn't implement the interface of a container (doesn't provide an iterator, etc). Instead, it provides limited access to a container. Those provided in the standard library operate using a sequence.
The container adapters are classes that provide a subset of a container's functionality but may provide additional functionality that makes it easier to use containers for certain scenarios.
These types of containers are called container adapters. The C++ Standard Library implements class templates such as stack, queue, and priority_queue as a container that puts constraints on the process of storage and retrieval of elements.
Container adaptors provide a different interface for sequential containers. stack: Adapts a container to provide stack (LIFO data structure) (class template). queue: Adapts a container to provide queue (FIFO data structure) (class template).
Just like Sequence and Associative Containers, there goal is to store objects in a particular manner with certain functionality. Container Adaptors take your regular containers (such as Sequence Containers) and limit functionality in such a way to create a new container type with new functionality.
The C++ Standard Library offers a host of implementations on common data structures and algorithms. The collection of container classes provides a set of data structure to store a list of objects as its element. The container classes are templatized to make it generic in the sense that it can store objects of any data type.
Well, I think this is because clear
was 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).
The only reason to use the container adaptor queue instead of the container deque is to make it clear that you are performing only queue operations, and no other operations. (from the sgi page on queue)
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.
Deque has clear(). See, e.g., http://www.cplusplus.com/reference/stl/deque/clear.html.
However, queue does not. But why would you choose queue over deque, anyway?
The only reason to use the container adaptor queue instead of the container deque is to make it clear that you are performing only queue operations, and no other operations.
(http://www.sgi.com/tech/stl/queue.html)
So I guess clear() is not a queue operation, then.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With