Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't the standard C++ container adaptors provide a clear function?

Tags:

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?

like image 366
Michael Kristofik Avatar asked Jan 29 '09 22:01

Michael Kristofik


People also ask

What is the difference between a container and a container adapter?

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.

What are containers Adaptors?

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.

What are container adapters in C++?

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.

What is the difference between container adaptors and queue adaptors?

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).

What is the difference between sequence and container adaptors?

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.

What is the use of container class in C++?

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.


2 Answers

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.

like image 177
Luc Touraille Avatar answered Nov 08 '22 04:11

Luc Touraille


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.

like image 36
Reunanen Avatar answered Nov 08 '22 03:11

Reunanen